Crack Mathmen
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
For example, if they choose n = 2 and the message is "World" (without quotation marks), they encode the message like this:
1. the first character is 'W', and it's ASCII code is 87. Then f(′W′) = 87^2 mod 997 = 590.
2. the second character is 'o', and it's ASCII code is 111. Then f(′o′) = 111^2 mod 997 = 357.
3. the third character is 'r', and it's ASCII code is 114. Then f(′r′) = 114^2 mod 997 = 35. Since 10 <= f(′r′) < 100, they add a 0 in front and make it 035.
4. the forth character is 'l', and it's ASCII code is 108. Then f(′l′) = 108^2 mod 997 = 697.
5. the fifth character is 'd', and it's ASCII code is 100. Then f(′d′) = 100^2 mod 997 = 30. Since 10 <= f(′d′) < 100, they add a 0 in front and make it 030.
6. Hence, the encrypted message is "590357035697030".
One day, an encrypted message a mathman sent was intercepted by the human being. As the cleverest one, could you find out what the plain text (i.e., the message before encryption) was?
输入
输出
示例输入
3
2
590357035697030
0
001001001001001
1000000000
001001001001001
示例输出
World
No Solution
No Solution
提示
来源
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
char a[];
char b[];
char map[]; //映射
int GetM(int t,int n) //快速幂求模
{
int ans = ;
while(n){
if(n & )
ans = (ans*t)%;
t=t*t%;
n>>=;
}
return ans;
}
bool GetMap(int n) //产生映射表
{
int c;
for(c=;c<=;c++){
int t = GetM(c,n);
if(map[t]!='\0') //该值已有对应的字母
return false;
map[t] = char(c);
}
return true;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
int i,n,len = ;
scanf("%d",&n);
scanf("%s",a);
memset(map,'\0',sizeof(map)); //初始化映射
if(!GetMap(n)){ //产生映射.如果失败,输出提示,退出本次循环
printf("No Solution\n");
continue;
}
//没有冲突,产生映射成功,根据映射表解码
int t = ;
int Len = strlen(a);
for(i=;i<Len;i+=){
t = (a[i]-'')* + (a[i+]-'')* + (a[i+]-'');
if(map[t]=='\0') //没有对应的映射
break;
b[len++] = map[t];
}
if(i<Len) //提前跳出
printf("No Solution\n");
else{
b[len] = '\0';
cout<<b<<endl;
}
}
return ;
}
Freecode : www.cnblogs.com/yym2013