当前位置: 代码迷 >> 综合 >> Lonlife-ACM 1005 - Spoon Devil's RP Test(同余定理)——“玲珑杯”acm比赛-试运行赛
  详细解决方案

Lonlife-ACM 1005 - Spoon Devil's RP Test(同余定理)——“玲珑杯”acm比赛-试运行赛

热度:16   发布时间:2023-12-12 09:18:10.0

此文章可以使用目录功能哟↑(点击上方[+])

 Lonlife-ACM 1005 - Spoon Devil's RP Test

Accept: 0    Submit: 0
Time Limit: 1s    Memory Limit : 32MByte

 Problem Description

Spoon Devil finds a way to test one person's RP: He defines 'a' = 1, 'b' = 2^2, ... 'z' = 26^2, so the value of 'abc' is 149, and the RP of 'abc' is the value of 'abc' mod 101. So the RP of 'abc' is 48.

 Input

The first line is a single integer T which is the number of test cases.

Each case only contains a name, which only contains lower-case letter.

 Output

For each test case, output is the RP of the name in one line.

 Sample Input

1
spoondevil

 Sample Output

78

 Hint

 Problem Idea

解题思路:

【题意】
定义'a' = 1, 'b' = 2^2, ... 'z' = 26^2

所以'abc'=149

'abc'的RP为149%101=48

现在给你一个由小写字母构成的人名

问该人名的RP值为多少

【类型】
同余定理
【分析】

由同余定理可得

①(a%m+b%m)%m≡(a+b)%m

②((a%m)*(b%m))%m≡(a*b)%m

故一个数,如567,当对4取模时,可以转化为(((5%4)*10+6)%4*10+7)%4

同理,此题就是利用这种转化,只是因为字符值不一定是一位数,故不仅仅会*10,可能还会*100,甚至*1000

【时间复杂度&&优化】
O(strlen(s))

题目链接→Lonlife-ACM 1005 - Spoon Devil's RP Test

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 100005;
const int M = 100005;
const int inf = 1000000007;
const int mod = 101;
char s[N];
int fun(int x)
{return x<10?10:x<100?100:1000;
}
int main()
{int t,k,i,ans;scanf("%d",&t);while(t--){scanf("%s",s);for(ans=i=0;s[i]!='\0';i++){k=s[i]-'a'+1;k*=k;ans=(ans*fun(k)+k)%mod;}printf("%d\n",ans);}return 0;
}
菜鸟成长记

  相关解决方案