SDUT 2165 Crack Mathmen(快速幂)山东省第二届ACM大学生程序设计竞赛
2016-04-25 07:44
423 查看
Crack Mathmen
Time Limit: 1000MS Memory limit: 65536K
题目描述
Since mathmen take security very seriously, they communicate in encrypted messages. They cipher their texts in this way: for every charactherc in the message, they replace c with f(c) = (the ASCII code of c)n mod 1997 if f(c) < 10, they put two preceding zeros in front
of f(c) to make it a three digit number; if 10 <= f(c) < 100, they put one preceding zero in front of f(c) to make it a three digit
number.
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?
输入
The input contains multiple test cases. The first line of the input contains a integer, indicating the number of test cases in the input.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 length of the string is at most 10^6.
输出
For each test case, output a line containing the plain text. 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
分析:题意是,对字母和数字进行编码,每个字母或数字的编码都是三位,编码规则为该字符的ASCII值的n次方mod 997。并保证对每个字符(只包括字母和数字)的编码存在且唯一,若译码结果不是字符或数字则输出No Solution
用快速幂求出相应的编码,再用编码做为数字下表存字符,例如ch[ 590 ]='W';
代码如下:
#include <stdio.h> #include <string.h> long long powerMod(long long a,long long b,long long c) { long long ans=1; a=a%c; while(b>0) { if(b%2==1) ans=(ans*a)%c; b=b/2; a=(a*a)%c; } return ans; } int main() { int i,j; int T; int b,c; long long n; char a[1005],str[1000005]; scanf("%d",&T); while(T--) { int peace=0; scanf("%I64d%*c",&n); memset(a,' ',sizeof(a)); for(i=0;i<26;i++) { long long ans=powerMod('a'+i,n,997);<span style="white-space:pre"> </span>//快速幂小写字母编码 if(a[ans]==' ')<span style="white-space:pre"> </span>//保证编码不重复 a[ans]='a'+i; else { peace=1; break; } } for(i=0;i<26 && !peace;i++) { long long ans=powerMod('A'+i,n,997);<span style="white-space:pre"> </span>//快速幂大写字母编码 if(a[ans]==' ') a[ans]='A'+i; else { peace=1; break; } } for(i=0;i<10 && !peace;i++) { long long ans=powerMod('0'+i,n,997);<span style="white-space:pre"> </span>//快速幂数字编码 if(a[ans]==' ') a[ans]='0'+i; else { peace=1; break; } } scanf("%s",str); if(!peace) { int len=strlen(str); for(i=0;i<len;i+=3) { int ans; ans=(str[i]-'0')*100+(str[i+1]-'0')*10+(str[i+2]-'0'); if(a[ans]!=' ') printf("%c",a[ans]); else { printf("No Solution"); break; } } putchar('\n'); } else { printf("No Solution\n"); } } return 0; }
相关文章推荐
- win10类型按钮
- 团队项目个人工作总结(4.25)
- 附视频及ppt:在线分享《从案例说InnoDB的基本优化》总结
- 查询MYSQL和查询HBASE速度比较
- 站立会议07
- 图片加载之Glide
- 个人工作总结07
- 第 2 章 基本格式
- 对输入法的评价
- 基于CefSharp构建基于Chromium的应用程序
- 【DDD/CQRS/微服务架构案例】在Ubuntu 14.04.4 LTS中运行WeText项目的服务端
- Gradle脚本的变量及其作用域
- Ubuntu 16.04 LTS安装好之后需要做的15件事
- [BZOJ1231][Usaco2008 Nov]mixup2 混乱的奶牛(状压dp)
- Ubuntu15.04 安装Geforce显卡驱动和CUDA
- SDUT 2164 Binomial Coeffcients 山东省第二届ACM大学生程序设计竞赛
- Spring AOP 实现机制
- SDUT 2163 Identifiers 山东省第二届ACM大学生程序设计竞赛
- 浅谈特定型人工智能设计
- OpenStack 通用设计思路 - 每天5分钟玩转 OpenStack(25)