The second session of the Shandong Province of ACM Review Crack mathmen (play table)

Source: Internet
Author: User

Crack Mathmen

Title Link: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2165

Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Title DescriptionSince Mathmen Take security very seriously, they communicate in encryptedmessages. They cipher their texts in this way:for every characther C in themessage, they replace C with f (c) = (the ASCII Code of c) n MoD 1997if f (c) <, they put, preceding zeros in front of F (c) to make it a three digit number, if <= f (c) <, they put one preceding zero in front off (c) to make it a three digit number.

For example, if they choose n = 2 and the message was "world" (without quotation marks), they encode t He 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 <= F (′r′) <, 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 <= F (′d′) <, 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 is intercepted by the Human Being. As the cleverest one, could you find out what is the plain text (i.e., the message before encryption) was?

InputThe input contains multiple test cases. the first line of the input containsA integer, indicating the number of test cases in the input. the first line ofEach test case contains a non-negative integer n (n <= 10^9). The second lineof each test case contains a string of digits. The length of the string is atMost 10^6.Outputfor each test case, output a line containing the plain text. If their is no orMore than one possible plain text so can be encrypted as the input and thenOutput "No solution" (without quotation marks). Since mathmen Use only alphebetical letters and digits, can assumeThat no characters other than alphebetical letters and digits could occur inThe plain text.Print a line between the test cases.Sample input
3259035703569703000010010010010011000000000001001001001001
Sample output
Worldno Solutionno Solution The first row 3, representing three sets of data, and then each group entered two lines the first row is n the second line is to decipher the password; Divide the password into a group of three numbers, and decipher for example the first set of sample 590357035697030 splits it into a group every 3 Each group is translated into one character, the first line is entered in the n=2, and several parties representing the ASC code for that character, such as 590 = 87^2%997, ' W ' ASC code is 87, so the first letter is W, and so on the output of the world, we can see that as long as we know the ASC code, we can find the corresponding Characters, it is easy to think of the table, because the title of the translated password only contains uppercase and lowercase letters and numbers, the array can be stored without opening a large, and for the power to take the model, the direct use of the Quick Power template is ok. Condition of no solution: 1: No corresponding character 2: the corresponding word than characters in a
#include <stdio.h> #include <cmath> #include <cstring> #include <stdlib.h>typedef 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)//fast Power Template {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, ' n ', sizeof (AR)), memset (str, ' + '), 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);///after entering T, the calculated value is stored to the coordinates of the character array ar, and the ASC code of that position is converted into a word designators to AC if (ar[res]== ' s)//used to mark if there is a duplicate password ar[res]= char (i);//Convert the location's ASC code into a word designators to AC else{signaa=1;//if repeated, the tag variable changes to 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];//every three words designators to another array, and of course int res= (str[i]-' 0 ') *100+ (str[i+1]-' 0 ') *10+str[i+2]-' 0 '; more simple}if (Signaa ) {printf ("No solution\n "); continue;} else{for (int i=1;i<=k;i++) {int res=atoi (test[i]), if (ar[res]!= ') printf ("%c", Ar[res]); else{signaa=1;break;}} if (SIGNAA) {printf ("No solution\n");} elseprintf ("\ n");}} return 0;}



The second session of the Shandong Province of ACM Review Crack mathmen (play table)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.