题意:给出p, q, e, l,令n = p * q, fn = (p-1) * (q-1)
给出l个c,计算m = D(c) = cd mod n,其中m为要输入的明文对应的ascii编码,d的计算方法:> calculate d, making d × e mod F(n) = 1 mod F(n), and d will be the private key。
问明文。
思路:
出题人JGShining(极光炫影)傻逼。
题意都说不清?
大小写字母一个意思?
脑袋有坑的出题人。
出题人傻逼。
出题人傻逼。
出题人傻逼。
好了。这道题需要用到扩展欧几里得算法求逆元。。。ksm(a,mod-2)的方法是基于费马小定理,必须mod为质数才可以用。扩展偶记里算法没有这个限制。
用欧几里德算法求模的逆元:
同余方程ax≡b (mod n),如果 gcd(a,n)== 1,则方程只有唯一解。
在这种情况下,如果 b== 1,同余方程就是 ax=1 (mod n ),gcd(a,n)= 1。
这时称求出的 x 为 a 的对模 n 乘法的逆元。
对于同余方程 ax= 1(mod n ), gcd(a,n)= 1 的求解就是求解方程
ax+ ny= 1,x, y 为整数。这个可用扩展欧几里德算法求出,原同余方程的唯一解就是用扩展欧几里德算法得出的 x 。
也就是gcd(a,n,x,y);
然后再(x = x%n + n)%n,得到最小的正整数x,即为a在模n意义下的逆元。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/* *********************************************** Author :111qqz Created Time :Wed 19 Oct 2016 04:51:40 PM CST File Name :code/hdu/1211.cpp ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdlib> #include <ctime> #define fst first #define sec second #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair using namespace std; const double eps = 1E-8; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; const int inf = 0x3f3f3f3f; LL p,q,e,l,d; LL n,fn; LL exgcd( LL a,LL b,LL &x,LL &y) { if (b==0) { x = 1; y = 0; return a; } LL ret = exgcd(b,a%b,y,x); y-=x*(a/b); return ret; } LL ksm( LL a,LL b,LL k) { LL res = 1; while (b>0) { if (b&1) res = (res * a)%k; b = b >> 1; a = (a * a) % k; } return res; } int main() { #ifndef ONLINE_JUDGE freopen("code/in.txt","r",stdin); #endif while (~scanf("%lld %lld %lld %lld",&p,&q,&e,&l)) { n = p * q; fn = (p-1) * (q-1); LL tmp; exgcd(e,fn,d,tmp); d = (d%fn + fn)%fn; // printf("d:%lld\n",d); for ( int i =1 ; i <= l ; i ++) { LL x; scanf("%lld",&x); LL val = ksm(x,d,n); // cout<<"val:"<<val<<endl; printf("%c",char(val)); } printf("\n"); } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } |
说点什么
您将是第一位评论人!