第二届山东省ACM省赛回顾 Crack Mathmen(打表)

Crack Mathmen

题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2165

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

第一行3,代表三组数据,然后每组输入两行 第一行是 n 第二行是要破译的密码;
把密码分成每三个数字一组,去破译

例如第一组样例 590357035697030 把它每3个拆成一组,每组翻译成一个字符,第一行输入的 n=2,代表该字符asc码的几次方
例如 590 = 87^2%997  , ‘W的‘asc码就是 87,, 所以第一个字母是 W,依次类推输出了 World;
可以看出只要我们知道了asc码,我们就能求出 对应的字符,很容易想到打表,因为题目说翻译后的密码只包含大小写字母和数字,数组不用开很大就能储存;
而 对于求幂取模,,直接套用快速幂模板就行了。 No Solution的情况: 1:没有对应的字符 2:对应的字符多于一个
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <stdlib.h>
typedef long long ll;
const int maxn=1000000+10;
char str[maxn];
char test[maxn/3][5];
char ar[1010];
int signaa;
ll pow_mod(ll x,ll n, ll mod) //快速幂模板
{
	ll res=1;
	x=x%mod;
	while(n>0)
	{
		if(n%2)
		{
			res=res*x%mod;
		}
		x=x*x%mod;
		n/=2;
	}
	return res;
}
int main()
{
	int n;
	scanf("%d",&n);
	int cas=0;
	while(n--)
	{
		signaa=0;
		memset(ar,'\0',sizeof(ar));
		memset(str,'\0',sizeof(str));
		int t;
		scanf("%d",&t);
		getchar();
		int i;int res;
		for(int i=48;i<=122;i++)
		{
			if((i>=48&&i<=57)||(i>=65&&i<=90)||(i>=97&&i<=122))
			{
				res=pow_mod(i,t,997);//输入了t之后,把求出的值储存到字符数组ar的坐标,把该位置的asc码转换成字符存到ac中
				if(ar[res]=='\0')  //用来标记是否有重复的密码
					ar[res]=char(i);//把该位置的asc码转换成字符存到ac中
				else
				{
					signaa=1;//如果重复,标记变量变为1;
					break;
				}
			}
		}
		gets(str);
		int len=strlen(str);
		int k=0,s=0;
		for(int i=0;i<len;i++)
		{
			if(i%3==0)
			{
				k++;
				s=0;
			}
			test[k][s++]=str[i];	//每三个字符存到另一个数组中,,当然也可以int res=(str[i]-'0')*100+(str[i+1]-'0')*10+str[i+2]-'0';更简单
		}
		if(signaa)
		{
			printf("No Solution\n");
			continue;
		}
		else
		{
			for(int i=1;i<=k;i++)
			{
				int res=atoi(test[i]);
				if(ar[res]!='\0')
					printf("%c",ar[res]);
				else
				{
					signaa=1;
					break;
				}
			}
			if(signaa)
			{
				printf("No Solution\n");

			}
			else
				printf("\n");
		}

	}
	return 0;
}

时间: 2024-08-27 13:18:57

第二届山东省ACM省赛回顾 Crack Mathmen(打表)的相关文章

第十届山东省acm省赛补题(2)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4124 L Median Time Limit: 1 Second      Memory Limit: 65536 KB Recall the definition of the median of  elements where  is odd: sort these elements and the median is the -th largest element.

第七届山东省ACM省赛

激动人心的省赛终于结束了…平静下来再回头看真的感觉一波三折…先是赛前毫无预兆的查出突发性耳聋…伴随而来的就是左耳听力下降.轻微耳鸣.极个别情况下的头晕…不过这都还好,毕竟药物可以恢复…热身赛只过了一道输出济南有多少泉水的水题,竟然第二次就猜对了,有个队交了四五十次…山师很心机的把酒店安排在了商业区.闹市和女子学院附近…(开个玩笑)为了不败第二天人品,我老老实实地待在了酒店,并没有出去嗨…正式赛开了…比赛打得多了,真的不紧张了…或许是喝了磊神指定饮料-红牛的作用…A是个签到题,我和Julyc讨论一

第十届山东省ACM省赛题解

点击跳转 A - Calandar H - Stones in the Bucket L - Median 原文地址:https://www.cnblogs.com/CSGOBESTGAMEEVER/p/10891007.html

2017年山东省ACM省赛总结

----但求努力到问心无愧 这次比赛我们是作为友谊队去的,本来我们队选拔赛成绩并不是很好,是去不了的,但伟大的教主大人牛逼地又要到了几个省赛友谊队的名额,才让我们有这次见识大场面比赛的机会,在这里我们先要感谢教主,还有就是感谢陪同的老师们,还有一直忙里忙外的负责人学长和同学们. 然后就是检讨我们自己了.这次比赛我们真的打的很不好,虽然比赛方有好多地方弄得有点欠缺.首先是热身赛,开始我们以为会有好多题,发下题目来看原来只有3个,好有三个题就三个题,那就做,但是我们还没开始看题,就意识到一个问题:这

山东省第五届省赛回顾 Full Binary Tree

Full Binary Tree 题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2882 Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 In computer science, a binary tree is a tree data structure in which each node has at most tw

南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 E

链接:https://www.nowcoder.com/acm/contest/122/E来源:牛客网 题目描述 愉快的周末到了,小C和他的N-1个朋友买了M个游戏,游戏编号从1~M.每个游戏都是多人游戏,他们打算周末一起打游戏. 小C的每个朋友都决定好了要玩哪一款游戏(会有一组人打同一款游戏),并且每人手上都有一台游戏机,这种游戏机可以通过特定的游戏机连接线连接起来. 但是,他们面临着一个问题:目前没有一个朋友的游戏机是互相连接的.所以它们必须用可用的游戏机连接线连接起来.小C决定依次使用第

【第六届山东省ACM竞赛】B题 Lowest Unique Price(SDUT3252)

题目链接:Here 这一题是我今年省赛最大的遗憾啊.诶...想想就觉得伤心啊.这一题其实不难,但是比赛时,我已经先到了怎么做,但是由于鄙人的失误,结果导致我们队后两个小时的时间都耗在那里了.越想越觉得可惜啊.我们现在看看这题的思路吧. 这一题,貌似大多人都是有STL的set做的.其实,这一题可以用线段树做(不知道线段树的童鞋请移步:这里),而且还是简单的单点更新问题.不知道单点更新的同学请移步:这里.虽然我做线段树的题目不是很多,但是我发现了一个规律.利用这个规律就可以更好的判断,此题是否为线段

第六届山东ACM省赛总结

省赛总结 省赛打完了,结果在意料之中,教主的判断还是很准的,我们的水平在银牌区中段,运气好可以进金牌区.感觉队伍打的还不错,感谢给力的队友,但感觉我个人还是打的有点乱. 开场我先简单整体的翻了一下习题册,依旧abc一人一题,我开b,李睿易a王成瑞c,b读读感觉是个模拟,尽管感觉不难,但是之前每逢模拟必卡题,就先跳过了,和王成瑞去看c,是个博弈,有点小自信,因为省赛前一段时间做过几道博弈,又看到有一队5min C题1Y,自信的推c了,第一次直接对3取余wa一发,李睿易看出a水,喊王成瑞去写a,我继

【第七届山东省ACM竞赛】Square Number

思路比较明确,就是一个数,如果和另外一个数乘起来是个平方数的话,那么满足一个条件 数A可以分解成为n1 个 a1,n2 个 a2 -- 数B可以分解成为m1个 a1,m2 个 a2-- 这满足的条件是(ni + mi) % 2 == 0 一个数的分解出来奇个数的因子乘起来得到的值为v,找之前有几个数他的奇个数因子成积为v 代码如下: #include<cmath> #include<cstdio> #include<cstring> #include<iostre