1. 程式人生 > 其它 >HDUOJ-------(1211)RSA

HDUOJ-------(1211)RSA

RSA

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1135    Accepted Submission(s): 833

Problem Description

RSA is one of the most powerful methods to encrypt data. The RSA algorithm is described as follow: > choose two large prime integer p, q > calculate n = p × q, calculate F(n) = (p - 1) × (q - 1) > choose an integer e(1 < e < F(n)), making gcd(e, F(n)) = 1, e will be the public key > calculate d, making d × e mod F(n) = 1 mod F(n), and d will be the private key You can encrypt data with this method : C = E(m) = me

mod n When you want to decrypt data, use this method : M = D(c) = cd mod n Here, c is an integer ASCII value of a letter of cryptograph and m is an integer ASCII value of a letter of plain text. Now given p, q, e and some cryptograph, your task is to "translate" the cryptograph into plain text.

Input

Each case will begin with four integers p, q, e, l followed by a line of cryptograph. The integers p, q, e, l will be in the range of 32-bit integer. The cryptograph consists of l integers separated by blanks.

Output

For each case, output the plain text in a single line. You may assume that the correct result of plain text are visual ASCII letters, you should output them as visualable letters with no blank between them.

Sample Input

101 103 7 11 7716 7746 7497 126 8486 4708 7746 623 7298 7357 3239

Sample Output

I-LOVE-ACM.

Author

JGShining(極光炫影)

Source

杭電ACM省賽集訓隊選拔賽之熱身賽

Recommend

Eddy

何為快速冪.....自己百度去...  自己根據以往經驗敲的...所以自己驗證去吧...

說下簡單的快速冪吧....

//求 ans=a的b次冪mod n
quick_pow(int a,int b,int ans,int n)
{
   while(a)
{
  if(a&1)
  {
   ans*=b;
   ans%=n;
    a--;
   }
   else
  {
    b*=b;
    b%=n;
    b>>=1;
  }
}
}

擴充套件歐幾里得程式碼:

 1   // ax+by=c;  c/q=k;(k=1,2,3,.....n)
 2    int x,y,q;
 3    void exgcd(int a ,int b)
 4    {
 5       if(b==0)
 6        {
 7         y=0,x=1,q=a;
 8         }
 9        else 
10        {
11           int temp=x;
12           x=y;
13          y=temp-a/b*y;
14         }
15     }

簡單題。。。。

程式碼:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define LL _int64  
 5 LL d,a,b;
 6 /*ax+by=c*/
 7 /*歐幾里得擴充套件*/
 8 void exgcd(LL x ,LL y)
 9 {
10   if(y==0)
11   {
12     a=1,b=0,d=x;
13   }
14   else
15   {
16    exgcd(y,x%y);
17    LL temp=a;
18    a=b,b=temp-x/y*b;
19   }
20 }
21 int main()
22 {
23     LL p,q,e,len,key;
24     while(scanf("%I64d %I64d %I64d %I64d",&p,&q,&e,&len)!=EOF)
25     {
26        exgcd(e,(p-1)*(q-1));
27        while(a<0)
28        {
29            a+=(p-1)*(q-1);
30        }
31            LL  n=p*q;
32         LL cnt=a;
33        while(len--)
34       {
35         scanf("%I64d",&key);
36          key%=n;
37         LL ans=1;
38        /*可以用快速冪*/
39         a=cnt;
40         while(a)
41         {
42             if(a&1)
43             {
44              ans*=key;
45               ans%=n;
46              a--;
47             }
48             else
49             {
50             key*=key;
51             key%=n;
52             a>>=1L;
53             }
54         }
55         putchar(ans);
56       }
57        putchar(10);
58     } 
59 return 0;
60 }