Lonlife-ACM 1005 - Spoon Devil's RP Test(同餘定理)——“玲瓏杯”acm比賽-試運行賽

此文章可以使用目錄功能喲↑(點擊上方[+])

 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;
}
菜鳥成長記
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章