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

时间:2022-09-18 20:57:03

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;
}

Sdut 2165 Crack Mathmen(数论)(山东省ACM第二届省赛E 题)的更多相关文章

  1. Sdut 2164 Binomial Coeffcients &lpar;组合数学&rpar; (山东省ACM第二届省赛 D 题)

    Binomial Coeffcients TimeLimit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 输入 输出 示例输入 1 1 10 2 9 ...

  2. ACM Sdut 2158 Hello World&excl;&lpar;数学题&comma;排序&rpar; &lpar;山东省ACM第一届省赛C题&rpar;

    题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...

  3. sdut 2165&colon;Crack Mathmen(第二届山东省省赛原题,数论)

    Crack Mathmen Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Since mathmen take securit ...

  4. Sdut 2151 Phone Numbers &lpar;山东省ACM第一届省赛题 A&rpar;

    题目描述 We know thatif a phone number A is another phone number B's prefix, B is not able to becalled. ...

  5. 山东省第七届省赛 D题:Swiss-system tournament(归并排序)

    Description A Swiss-system tournament is a tournament which uses a non-elimination format. The first ...

  6. 山东省第六届省赛 H题:Square Number

    Description In mathematics, a square number is an integer that is the square of an integer. In other ...

  7. sdut2165 Crack Mathmen (山东省第二届ACM省赛)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/24270265 本文出自:http://blog.c ...

  8. ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)

    Problem Description Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109 ...

  9. 2013 ACM&sol;ICPC 长春网络赛F题

    题意:两个人轮流说数字,第一个人可以说区间[1~k]中的一个,之后每次每人都可以说一个比前一个人所说数字大一点的数字,相邻两次数字只差在区间[1~k].谁先>=N,谁输.问最后是第一个人赢还是第 ...

随机推荐

  1. windows

    1.拷贝远程文件 net use \\10.130.80.62\ipc$ 密码 /user:用户名 xcopy "\\10.130.80.62\G$\yt\apache-tomcat-7.0 ...

  2. 访客至上的Web、移动可用性设计--指导原则

    文章出自:听云博客 关于可用性设计,之前写过一个“纸上谈兵”版本的,那篇帖子主要是根据A/B test的方式来进行的. 但是最近找了本Steve krug写的Don't make me think,我 ...

  3. First Blog&comma; &ldquo&semi;Hello&comma; world&excl;&rdquo&semi;

    As every single book says as a tradition, "Hello, world!" An explanation about the name – ...

  4. 受限玻尔兹曼机(RBM)学习笔记(五)梯度计算公式

      去年 6 月份写的博文<Yusuke Sugomori 的 C 语言 Deep Learning 程序解读>是囫囵吞枣地读完一个关于 DBN 算法的开源代码后的笔记,当时对其中涉及的算 ...

  5. FMDB 二次封装工具类,让你快速学会封装,集成数据库

    来源:StrivEver 链接:http://www.jianshu.com/p/4c77aee0b41c 上个版本为了增加用户体验,部分页面集成了离线缓存数据功能,于是就在项目里使用了数据库管理离线 ...

  6. &lbrack;Linux&rsqb;root默认密码问题

    Linux/Ubuntu root默认密码问题   Ubuntu安装后root是没有默认的密码的,因为你还没给root设置密码,你第一个 user 是在 admin 组,他可以给 root 设置密码, ...

  7. php的一些小笔记--时间函数

    strtotime  返回UNIX时间戳 .这个函数可以用来计算前天,昨天,后天,明天 例如明天:date('Y-m-d H:is',strtotime('+1 day')) day>1是复数 ...

  8. 分享一个基于thrift的java-rpc框架

    简单介绍 这是一个简单小巧的Java RPC框架,适用于Java平台内.为系统之间的交互提供了.高性能.低延迟的方案.适合在集群数量偏少的情况下使用(50台以下集群环境).当然.它也可以在大型集群环境 ...

  9. Java开发笔记(二十三)数组工具Arrays

    数组作为一种组合形式的数据类型,必然要求提供一些处理数组的简便办法,包括数组比较.数组复制.数组排序等等.为此Java专门设计了Arrays工具,该工具包含了几个常用方法,方便程序员对数组进行加工操作 ...

  10. &lbrack;ERROR&rsqb; Failed to execute goal org&period;apache&period;maven&period;plugins&colon;maven-compiler-plugin&colon;3&period;2&colon;compile &lpar;default-compile&rpar; on project taotao-manager-pojo&colon; Compilation failure

    运行maven项目时报错 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compi ...