sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)

时间:2022-09-18 20:56:51

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 characther c 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 encrypted as 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

提示

 

来源

 山东省第二届ACM大学生程序设计竞赛

 
  数论
  需要用到快速求幂二分法求幂),因为n <= 10^9,所以依次相乘求幂的方法会超时。
  另外可以用字母转换后的值为映射数组的下标,这样查找的时候,直接就可以找到该值对应的字母。
  快速求幂的地方多写了一个else,没注意到调试了2个小时,可见一个小小的粗心导致的错误能浪费多少宝贵的时间!特别是在比赛中,一定要注意细心!一味的求速度只会导致功亏一篑。
  参考两位小伙伴的博客:
  Vit     CYll
  代码:
 #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

sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)的更多相关文章

  1. sdut 2163&colon;Identifiers(第二届山东省省赛原题,水题)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Identifier is an important c ...

  2. sdut 2162&colon;The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  3. sdut 2152&colon;Balloons(第一届山东省省赛原题,DFS搜索)

    Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...

  4. sdut 2153&colon;Clockwise(第一届山东省省赛原题,计算几何&plus;DP)

    Clockwise Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Saya have a long necklace with ...

  5. sdut 2154&colon;Shopping(第一届山东省省赛原题,水题)

    Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...

  6. sdut 2159&colon;Ivan comes again&excl;(第一届山东省省赛原题,STL之set使用)

    Ivan comes again! Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...

  7. sdut 2158&colon;Hello World&excl;(第一届山东省省赛原题,水题,穷举)

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...

  8. sdut 2610&colon;Boring Counting(第四届山东省省赛原题,划分树 &plus; 二分)

    Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you a ...

  9. sdut 2411&colon;Pixel density(第三届山东省省赛原题,字符串处理)

    Pixel density Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Pixels per inch (PPI) or pi ...

随机推荐

  1. Shell 编程基础之 While 练习

    一.语法 while [ condition ] # 当 condition 条件成立时,就进行循环,直到条件不成立停止 do #执行内容 done 二.练习 输入用户输入的参数,直到用户输入 &qu ...

  2. Visiual Studio2012 CLR20r3问题

    看到有更新,习惯性的点了,升级到Visiual Studio Ultimate 2012 Update 1,并且按照提升重启了电脑.因为昨天太晚,也没验证.尽早打开VS,结果直接Crash.错误如下: ...

  3. Chinese culture

      文房四宝 笔墨纸砚是中国古代文人书房中必备的宝贝,被称为“文房四宝”.用笔墨书写绘画在 中国可追溯到五千年前.秦(前221---前206)时已用不同硬度的毛和竹管制笔:汉代(前206—公元220) ...

  4. 8&period;HBase In Action 第一章-HBase简介(1&period;2&period;2 捕获增量数据)

    Data often trickles in and is added to an existing data store for further usage, such as analytics, ...

  5. 【海岛帝国系列赛】No&period;5 海岛帝国:独立之战

    50229234海岛帝国:独立之战 [试题描述] *多年来一直如饥似渴地渴求“药师傅”帝国,但是,“里脊肉”BANNIE时刻在守护着这一方水土.从而使帝国日益强大.如今,BANNIE由于在 “牡 ...

  6. UVa11248 Frequency Hopping(最大流&plus;最小割)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33206 [思路] 最大流最小割. 可以确定的是如果不可行需要修改的 ...

  7. 《Java从入门到放弃》JavaSE篇:程序结构

    程序的结构一般分为三种: 顺序结构. 选择结构. 循环结构. 一.顺序结构:这个不用多说吧,跟我们平时写文章的顺序一样,从上往下. 二.选择结构:从名字就能看出,要选择嘛,到底是要漂亮滴妹子,还是要有 ...

  8. spring各个版本开发包下载

    spring各个开发包版本下载地址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/o ...

  9. &period;net相关知识

    1.简述 private. protected. public. internal 修饰符的访问权限. private :   私有成员, 在类的内部才可以访问. protected : 保护成员,该 ...

  10. ORACLE RAC 11&period;2&period;0&period;4 CentOS release 6&period;9 静默安装1&period;0版本

    RAC11.2.0.4静默安装 1.0版本,20180613 #本文档IP地址使用X隐藏,个人可按照自己的当前环境IP进行适当修改 1. 清除原环境中的单实例软件 #清除原环境: 删除/etc/ora ...