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

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

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

我写的时候有一点没想通,也是非常关键的一点,加密算法  原码 转换到  加password,加password会出现反复的情况,这个我没推断到,后开在小伙伴的解释下,瞬间顿悟,然后,恩就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')    //  推断 原码 ->加password 转换过程中是否反复,反复则直接返回falsear[cal(i,n)] = char(i);  //  加password 作数组下标,匹配时直接寻找,无需查找elsereturn 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];}}elsenow = false;if(n==0)now = false;  //  n = 0 时 肯定为 No Solutionif(now)cout<<br<<endl;elsecout<<"No Solution"<<endl;}return 0;
}

转载于:https://www.cnblogs.com/mengfanrong/p/3820068.html

Sdut 2165 Crack Mathmen(数论)(山东省ACM第二届省赛E 题)相关推荐

  1. ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)

    ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题) 题目描述 We know thatIvan gives Saya three problems t ...

  2. HNUCM信息科学与工程学院第二届新生赛——正式赛

    HNUCM信息科学与工程学院第二届新生赛--正式赛 简单题 A:Yftc的字符串转换 题目描述 Ytfc来到了一个魔法密林,里面住着一只魔法兔子,Yftc想去见见这只魔法兔子,但是这个魔法密林很奇怪, ...

  3. HNUCM信息科学与工程学院第二届新生赛——热身赛

    HNUCM信息科学与工程学院第二届新生赛--热身赛 题目比较坑点较多,注意避坑,特别是数据范围和题意,因为是新生赛,所以还是选择使用了c语言书写,基本没有调用内置函数库,排序统一使用了冒泡排序,没有使 ...

  4. 2020年第十一届蓝桥杯C/C++ B组第二场省赛真题

    2020年第十一届蓝桥杯C/C++ B组第二场省赛真题 第一题 门牌制作 题目描述 小蓝要为一条街的住户制作门牌号.这条街一共有2020位住户,门牌号从1到2020编号.小蓝制作门牌的方法是先制作0到 ...

  5. 蓝桥杯|2021第十二届蓝桥杯第二次模拟赛真题与解答[Java]

    记录2021年第十二届蓝桥杯第二次模拟赛真题与解题,部分答案来自网络.个人记录,菜得很. 目录 填空题 一 二 三 四 五 编程题 六 七 八 九 十 填空题 一 问题描述 请问在 1 到 2020 ...

  6. 第二届八一赛考试专用答题卡与草稿纸

    因比赛批卷方便,特制作考试专用答题卡与草稿纸\color{red}专用答题卡与草稿纸专用答题卡与草稿纸 ,这里需要强调的是提交解答只需要将答题卡写的过程和结果通过扫描王APP转成pdf发送指定邮箱可作 ...

  7. HNUCM湖南中医药大学信息科学与工程学院第二届新生赛

    正式赛 目录 正式赛 问题 A: Yftc的字符串转换 问题 B: XP的午餐 问题 C: XP的电灯 问题 D: XP的宝藏 问题 E: 小小yh的几何 问题 F: 我爱中国 问题 G: 小小yh的 ...

  8. HDU - 5877 Weak Pair 2016 ACM/ICPC 大连网络赛 J题 dfs+树状数组+离散化

    题目链接 You are given a rootedrooted tree of NN nodes, labeled from 1 to NN. To the iith node a non-neg ...

  9. HDU - 5875 2016 ACM/ICPC 大连网络赛 H题 暴力

    题目链接 题意:给你一个区间l,r一直将val[l]模上val[l+1],val[l+2]...val[r],因为一个模上比前一个数小数是没有意义的,所以需要将每一个点找到右边第一个小于他的点就行. ...

最新文章

  1. 图形驱动程序和显卡驱动什么区别_我们常说的计算机驱动程序到底是什么,深入解读驱动程序本质...
  2. Node.js之readline模块的使用
  3. 2021牛客多校1 - Find 3-friendly Integers(数位dp)
  4. web架构设计经验分享(转)
  5. ionic滚动条返回顶部
  6. 高并发架构系列:分布式锁的由来、特点及Redis分布式锁的实现详解
  7. 配置国内免费registry mirror -daocloud、网易、阿里云和七牛
  8. LPC1788 nand驱动
  9. mvn 命令上传文件到 Maven 仓库
  10. 31个让你大呼惊艳的数据可视化作品!
  11. C语言进阶 之 字符串函数(4)
  12. [开源免费] ZXTouch - iOS11-14 iOS13 模拟触摸 模拟点击 比触动精灵和AutoTouch更好用的插件
  13. 使用Microsoft.Office.Interop.Excel批量编辑Excel文件
  14. 手机摄像头当电脑摄像头的软件
  15. 完整的渗透测试靶场通关
  16. 2022年,谁在推动产业数字化进入“奇点”时刻?
  17. npm install命令运行中出现Error: ENOENT: no such file or directory解决方法
  18. 八皇后问题理解与解决
  19. cocos creator利用ShareSDK实现微信登录功能
  20. 证书关于 pem der cer crt csr pfx 的区别

热门文章

  1. Python 实现针对时间序列预测的特征选择
  2. 【uC/OS-II】笔记1----入门
  3. HttpClient简介
  4. 1711: [Usaco2007 Open]Dingin吃饭
  5. oracle学习笔记之 审计
  6. flex3+blazeds+spring+hibernate整合小结
  7. Java 编解码问题
  8. raspberry pi_如何使用Raspberry Pi构建WiFi相框
  9. vco为什么低频下起振困难_为什么开放如此困难?
  10. 组织需要什么样的我_为什么开放组织对我说话