zoukankan      html  css  js  c++  java
  • HDU 1211 EXGCD

    EXGCD的模板水题

    RSA算法
    给你两个大素数p,q
    定义n=pq,F(n)=(p-1)(q-1)
    找一个数e 使得(e⊥F(n))
    实际题目会给你e,p,q
    计算d,$de mod F(n) = 1$
    然后解密的值为$c_{i}^d mod n$,转换成char输出 用EXGCD求出d就好了

    /** @Date    : 2017-09-07 22:17:00
      * @FileName: HDU 1211 EXGCD.cpp
      * @Platform: Windows
      * @Author  : Lweleth (SoungEarlf@gmail.com)
      * @Link    : https://github.com/
      * @Version : $Id$
      */
    #include <bits/stdc++.h>
    #define LL long long
    #define PII pair<int ,int>
    #define MP(x, y) make_pair((x),(y))
    #define fi first
    #define se second
    #define PB(x) push_back((x))
    #define MMG(x) memset((x), -1,sizeof(x))
    #define MMF(x) memset((x),0,sizeof(x))
    #define MMI(x) memset((x), INF, sizeof(x))
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    const int N = 1e5+20;
    const double eps = 1e-8;
    
    LL exgcd(LL a, LL b, LL &x, LL &y)
    {
        LL d = a;
        if(b == 0)
        {
            x = 1;
            y = 0;
        }
        else
        {
            d = exgcd(b, a % b, y, x);
            y -= (a / b)*x;
        }
        return d;
    }
    
    LL fpow(LL a, LL n, LL mod)
    {
    	LL res = 1;
    	while(n)
    	{
    		if(n & 1)
    			res = (res * a % mod + mod) %mod;
    		a = (a * a % mod + mod) % mod;
    		n >>= 1;
    	}
    	return res;
    }
    LL p, q, e, n;
    LL a[N];
    int main()
    {
    	while(~scanf("%lld%lld%lld%lld", &p, &q, &e, &n))
    	{
    		for(int i = 0; i < n; i++)	scanf("%lld", a + i);
    		LL mod = p * q;
    		LL fn = (p - 1) * (q - 1);
    		for(int i = 0; i < n; i++)
    		{
    			LL d = 0 , y = 0;
    			exgcd(e, fn, d, y);
    			d = (d + fn) % fn;
    			a[i] %= mod;
    			LL ans = fpow(a[i], d, mod);
    			printf("%c", fpow(a[i], d, mod) % mod);
    		}
    		printf("
    ");
    	}
        return 0;
    }
    
  • 相关阅读:
    Android 系统日期时间的获取
    Android What is Application
    Android 所有颜色代码
    Android onNewIntent
    Android Activity管理类
    Android 应用启动渐变效果
    算法的的代价及其度量
    算法的设计与分析
    算法的描述
    数据结构与算法(python版)教程
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/7496425.html
Copyright © 2011-2022 走看看