Crack Mathmen

TimeLimit: 1000ms   Memory
limit: 65536K  有疑问?点这里^_^

题目描述

Since mathmen take security very seriously, theycommunicate in encrypted messages. They cipher their texts
in this way: for everycharacther c in the message, they replace c with f(c) = (the ASCII code ofc)n mod 1997 if f(c)
< 10, they put two preceding zeros in front off(c) to make it a three digit number; if 10 <= f(c) < 100, they put onepreceding zero in front of f(c)
to make it a three digit number.

For example, if they choose n = 2 and themessage is "World" (without quotation marks), they encode themessage like this:

1. the first character is 'W', and it'sASCII code is 87. Then f(′W′) =87^2 mod
997 = 590.

2. the second character is 'o', and it'sASCII code is 111. Then f(′o′) = 111^2mod
997 = 357.

3. the third character is 'r', and it'sASCII 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'sASCII code is 108. Then f(′l′) =108^2 mod
997 = 697.

5. the fifth character is 'd', and it'sASCII 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 mathmansent was intercepted by the human being. As the cleverest one, could youfind out what the plain text (i.e., the message before encryption) was?

输入

The input contains multiple test cases. The first line ofthe input contains a integer, indicating the number of test cases in theinput. The first line of each
test case contains a non-negative integer n (n <=10^9). The second line of each test case contains a string of digits. The lengthof the string is at most
10^6.

输出

For each test case, output a line containing the plaintext. If their are no or more than one possible plain
text that can be encryptedas the input, then output "No Solution" (without quotation marks). Since mathmen use only
alphebetical letters and digits, you can assume that no characters other than alphebetical letters and digits may occur in the
plain text. Print a line between two test cases.

示例输入

3

2

590357035697030

0

001001001001001

1000000000

001001001001001

示例输出

World

No Solution

No Solution

/*************************

一道很高大上的数论题,开始看的一道  大神的,用数论的方法解的:http://limyao.com/?p=113#comment-111

大神用的有素数原根,完全剩余系,离散对数,模线性方程,知识点很多,也很难。。

有点小困难,然后我和小伙伴修昊讨论了下,觉得最初的想法——打表应该可以,然后就付诸行动了。。

我写的时候有一点没想通,也是很关键的一点,加密算法  原码 转换到  加密码,加密码会出现重复的情况,这个我没判断到,后开在小伙伴的解释下,瞬间顿悟,然后,恩就A了。

**********************/

Code:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
char ar[1000],br[340000],str[10000005];
int cal(int temp,int t)//位运算快速幂模
{
int ans = 1;
while(t)
{
if(t&1)
ans = (ans * temp) % 997;
temp = temp * temp % 997;
t = t >> 1;
}
return ans;
} bool init(int n)
{
memset(ar,'\0',sizeof(ar));
int i,tmp;
for(i = 32;i<=126;i++) // ASCII 码 打表,
{
if(ar[cal(i,n)]=='\0') // 判断 原码 ->加密码 转换过程中是否重复,重复则直接返回false
ar[cal(i,n)] = char(i); // 加密码 作数组下标,匹配时直接寻找,无需查找
else
return false;
}
return true;
} int main()
{
int n,c,i,j,len,cur;
bool now;
cin>>c;
while(c--)
{
now = true;
memset(br,'\0',sizeof(br));
cin>>n;
cin>>str;
len = strlen(str);
j = 0;
if(init(n))
{
for(i = 0;i<len;i+=3)
{
cur = (str[i]-'0') * 100 + (str[i+1]-'0') * 10 + str[i+2] - '0';
if(ar[cur] == '\0')
{
now = false;
break;
}
br[j++] = ar[cur];
}
}
else
now = false;
if(n==0)
now = false; // n = 0 时 肯定为 No Solution
if(now)
cout<<br<<endl;
else
cout<<"No Solution"<<endl;
}
return 0;
}

最新文章

  1. React的使用与JSX的转换
  2. 妙方之解决matplotlib的图例里的中文呈现小方形
  3. MUI - 上拉刷新/下拉加载
  4. git 代码组织
  5. js弹出窗口总结6种弹窗方法
  6. java map 遍历
  7. CentOS6.5_python2.7.3下virt-manager无法启动
  8. 基于JavaScript的REST客户端框架
  9. C语言使用正则表达式
  10. windows 下nginx 虚拟主机搭建
  11. PXE+kickstart无人值守安装CentOS 6
  12. Android之View绘制流程开胃菜---setContentView(...)详细分析
  13. Java内部类的总结
  14. 32位系统装4G以上的内存
  15. java类定义、变量类型、构造函数
  16. python获取操作系统平台、版本及架构
  17. 关于WPF中Popup中的一些用法的总结
  18. 1、JUC--volatile 关键字-内存可见性
  19. 测试leader职责
  20. 《完全用Linux工作》作者:王垠

热门文章

  1. 伪分布配置完成启动jobtracker和tasktracker没有启动
  2. C 数据结构1——线性表分析(顺序存储、链式存储)
  3. 问题-[DelphiXE2]提示第三控件不存在
  4. 教程-delphi的开源json库:superobject,用法简介
  5. 极客技术专题【008期】:CSS3核心技术:选择器
  6. IOI1998 hdu1828 poj1177 Picture
  7. 【Android - 进阶】之事件分发机制
  8. CSS Grid layout布局
  9. 动态调用DLL函数有时正常,有时报Access violation的异常
  10. LINQ to JavaScript