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;
}
菜鸟成长记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值