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.

示例输入

  1. 3
  2. 2
  3. 590357035697030
  4. 0
  5. 001001001001001
  6. 1000000000
  7. 001001001001001

示例输出

  1. World
  2. No Solution
  3. No Solution

提示

 

来源

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

 
  数论
  需要用到快速求幂二分法求幂),因为n <= 10^9,所以依次相乘求幂的方法会超时。
  另外可以用字母转换后的值为映射数组的下标,这样查找的时候,直接就可以找到该值对应的字母。
  快速求幂的地方多写了一个else,没注意到调试了2个小时,可见一个小小的粗心导致的错误能浪费多少宝贵的时间!特别是在比赛中,一定要注意细心!一味的求速度只会导致功亏一篑。
  参考两位小伙伴的博客:
  Vit     CYll
  代码:
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string.h>
  4. using namespace std;
  5. char a[];
  6. char b[];
  7. char map[]; //映射
  8. int GetM(int t,int n) //快速幂求模
  9. {
  10. int ans = ;
  11. while(n){
  12. if(n & )
  13. ans = (ans*t)%;
  14. t=t*t%;
  15. n>>=;
  16. }
  17. return ans;
  18. }
  19. bool GetMap(int n) //产生映射表
  20. {
  21. int c;
  22. for(c=;c<=;c++){
  23. int t = GetM(c,n);
  24. if(map[t]!='\0') //该值已有对应的字母
  25. return false;
  26. map[t] = char(c);
  27. }
  28. return true;
  29. }
  30. int main()
  31. {
  32. int T;
  33. scanf("%d",&T);
  34. while(T--){
  35. int i,n,len = ;
  36. scanf("%d",&n);
  37. scanf("%s",a);
  38. memset(map,'\0',sizeof(map)); //初始化映射
  39. if(!GetMap(n)){ //产生映射.如果失败,输出提示,退出本次循环
  40. printf("No Solution\n");
  41. continue;
  42. }
  43. //没有冲突,产生映射成功,根据映射表解码
  44. int t = ;
  45. int Len = strlen(a);
  46. for(i=;i<Len;i+=){
  47. t = (a[i]-'')* + (a[i+]-'')* + (a[i+]-'');
  48. if(map[t]=='\0') //没有对应的映射
  49. break;
  50. b[len++] = map[t];
  51. }
  52. if(i<Len) //提前跳出
  53. printf("No Solution\n");
  54. else{
  55. b[len] = '\0';
  56. cout<<b<<endl;
  57. }
  58. }
  59. return ;
  60. }

Freecode : www.cnblogs.com/yym2013

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

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

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

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

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

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

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

  4. sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)

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

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

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

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

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

  7. sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)

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

  8. sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)

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

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

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

随机推荐

  1. Laravel教程 八:queryScope 和 setAttribute

    Laravel教程 八:queryScope 和 setAttribute 此文章为原创文章,未经同意,禁止转载. Laravel Eloquent Database 直接就是按照上一节所说的那样,我 ...

  2. 工具分享——将C#文档注释生成.chm帮助文档

    由于最近需要把以前的一个项目写一个文档,但一时又不知道写成怎样的,又恰好发现了可以生成chm的工具,于是乎我就研究了下,感觉还不错,所以也给大家分享下.好了,不多废话,下面就来实现一下吧. 生成前的准 ...

  3. [转载]高效使用matlab之四:一个加速matlab程序的例子

    原文地址:http://www.bfcat.com/index.php/2012/11/speed-up-app/ 这篇文章原文是matlab网站上的,我把它翻译过来同时自己也学习一下.原文见这里 这 ...

  4. Openresty 与 Tengine

    Openresty 与 Tengine Openresty和Tengine基于 Nginx 的两个衍生版本,某种意义上他们都和淘宝有关系,前者是前淘宝工程师agentzh主导开发的,后者是淘宝的一个开 ...

  5. linux下防火墙开启某个端口号及防火墙常用命令使用

    linux防火墙常用命令 1.永久性生效,重启后不会复原 开启:chkconfigiptables on 关闭:chkconfigiptables off 2.即时生效,重启后复原 重启防火墙 方式一 ...

  6. 新浪微博客户端(24)-计算原创微博配图frame

    DJStatus.h #import <Foundation/Foundation.h> @class DJUser; /** 微博 */ @interface DJStatus : NS ...

  7. motto3

    在我看来,最努力的人不一定能收获最好的,但不努力的人是必定收获不到任何东西的. 所以,园主,你要会努力才行.否则,你会累死的. 用心去学,用脑去想,认真对待每一件事,聪明一点,不要太愚蠢.

  8. MySQL获取随机数

    如何通过MySQL在某个数据区间获取随机数? MySQL本身提供一个叫rand的函数,返回的v范围为0 <= v < 1.0. 介绍此函数的MySQL文档也介绍道,可以通过此计算公式FLO ...

  9. mstsc局域网远程 要预先做的设置

    很简单========= 一:在“控制面板”->“管理工具”->“服务”上启动Remote Desktop Help Session Manager的服务; 二: 在“控制面板”-> ...

  10. Android如何在java代码中设置margin

    习惯了直接在xml里设置margin(距离上下左右都是10dip),如: <ImageView android:layout_margin="10dip" android:s ...