POJ 2447 RSA 大数分解+逆元+快速幂

链接:http://poj.org/problem?id=2447

题意:

思路:Pollard_Rho质数分解,得到两个素数因子,P,Q,求出T,E,快速幂即可得M。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <set>
#include <ctime>
#define PI acos(-1.0)
#define maxn 10005
#define INF 0x7fffffff
#define eps 1e-8
#define seed 31
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    LL r=extend_gcd(b,a%b,x,y);
    LL t=x;
    x=y;
    y=t-a/b*y;
    return r;
}
LL mul_mod(LL a,LL b,LL n)
{
    a=a%n;
    b=b%n;
    LL s=0;
    while(b)
    {
        if(b&1)
            s=(s+a)%n;
        a=(a<<1)%n;
        b=b>>1;
    }
    return s;
}
LL pow_mod(LL a,LL b,LL n)
{
    a=a%n;
    LL s=1;
    while(b)
    {
        if(b&1)
            s=mul_mod(s,a,n);
        a=mul_mod(a,a,n);
        b=b>>1;
    }
    return s;
}
LL gcd(LL a,LL b)
{
    if(a==0) return 1;
    if(a<0) return gcd(-a,b);
    return b==0?a:gcd(b,a%b);
}
LL inv(LL a,LL m)
{
    LL d,x,y;
    d=extend_gcd(a,m,x,y);
    if (d==1)
    {
        x=(x%m+m)%m;
        return x;
    }
    else return -1;
}
LL Pollard_Rho(LL n)
{
    if(!(n&1)) return 2;
    while(true)
    {
        LL x=(LL)rand()%n;
        if(x<0)
            x=-x;
        LL y=x;
        LL c=(LL)rand()%n;
        if(x<0)
            c=-c;
        if(c==0||c==2) c=1;
        for(int i=1,k=2;; i++)
        {
            x = mul_mod(x,x,n);
            if (x >= c) x -= c;
            else x += n - c ;
            if (x == n) x = 0 ;
            if (x == 0) x = n-1;
            else x --;
            LL d = gcd (x>y ? x-y: y-x, n);
            if (d == n) break ;
            if (d != 1) return d ;
            if (i == k)
            {
                y = x;
                k <<= 1 ;
            }
        }
    }
}
int main()
{
    LL C,E,N;
    while(~scanf("%lld%lld%lld",&C,&E,&N))
    {
        LL aa=Pollard_Rho(N);
        LL T=(aa-1)*(N/aa-1);
        LL D=inv(E,T);
        LL M=pow_mod(C,D,N);
        printf("%lld\n",M);
    }
    return 0;
}

POJ 2447 RSA 大数分解+逆元+快速幂

时间: 2024-11-04 09:45:47

POJ 2447 RSA 大数分解+逆元+快速幂的相关文章

POJ 1845 - Sumdiv ( 数论 + 唯一分解定理 + 快速幂取模 )

POJ 1845 - Sumdiv ( 数论 + 唯一分解定理 + 快速幂取模 ) 这是一道数论的好题,需要较好的数学基础 题意: 给定A,B,求A^B的所有因数的和,再MOD 9901 分析: 这里用到了数论当中相当一部分知识 a. 唯一分解定理 任何一个整数都可以分解为若干个素数的幂的乘积的形式 A = ( p1 ^ q1 + p2 ^ q2 + ..... + pn ^ qn ) p为素数 A^B = ( p1 ^ (q1*B) + p2 ^ (q2*B) + ..... + pn ^ (

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

poj 2888 Magic Bracelet(Polya+矩阵快速幂)

Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4990   Accepted: 1610 Description Ginny’s birthday is coming soon. Harry Potter is preparing a birthday present for his new girlfriend. The present is a magic bracelet which

poj 2429 Pollard_rho大数分解

先对lcm/gcd进行分解,问题转变为从因子中选出一些数相乘,剩下的数也相乘,要求和最小. 这里可以直接搜索,注意一个问题,由于相同因子不能分配给两边(会改变gcd)所以可以将相同因子合并,这样的话,搜索的层数也变的很少了. #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<stdlib.h> #include<time.h&g

POJ 3744 Scout YYF I 矩阵快速幂

Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4452   Accepted: 1159 Description YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's base. After overcoming a series difficulties,

poj 3150 Cellular Automaton(矩阵快速幂)

http://poj.org/problem?id=3150 大致题意:给出n个数,问经过K次变换每个位置上的数变为多少.第i位置上的数经过一次变换定义为所有满足 min( abs(i-j),n-abs(i-j) )<=d的j位置上的数字之和对m求余. 思路: 我们先将上述定义表示为矩阵 B =  1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 1 1 B[i][j] = 表示i与j满足上述关系,B[i][j] = 0表示i与j不满足上述关系.根据这个

POJ 3735 Training little cats 矩阵快速幂应用

点击打开链接 Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9807   Accepted: 2344 Description Facer's pet cat just gave birth to a brood of little cats. Having considered the health of those lovely cats, Facer decides to

【POJ 3070】Fibonacci(矩阵快速幂)

[POJ 3070]Fibonacci(矩阵快速幂) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12333   Accepted: 8752 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the

2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂

原题:ZOJ 3774  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 ---------------------------------------------------------------------------------------------------------------------- 这题比较复杂,看这篇比较详细:http://blog.csdn.net/acdreamers/artic