hdu 1211 RSA


#include <stdio.h>

 

typedef long long LL;

LL p,q,e,n,phin,x,y;

void ext_gcd(LL a, LL b) // gcd(a,b) = ax+by
{
    if(!b)
    {
        x = 1;
        y = 0;
        return;
    }
    ext_gcd(b, a%b);
    LL t = x;
    x = y;
    y = t - ( a / b) * y;
}

LL exp_mod(LL a, LL b, LL m) //a^b % m
{
    LL s = 1;
    int i=0,j;
    bool bit[64];
    while(b)
    {
        bit[i++] = b&1 ? 1 : 0;
        b >>= 1;
    }
    for(j=i-1;j>=0;j--)
    {
        s = s * s % m;
        if(bit[j]) s = s * a % m;
    }
    return s;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("tdata.txt","r",stdin);
#endif
    int l;
    char ch;
    while(scanf("%I64d %I64d %I64d %d",&p,&q,&e,&l)!=EOF)
    {
        n = p * q;
        phin = (p-1) * (q-1);
        ext_gcd(e,phin); //d=x
        x = (x + phin) % phin; //d=x need to be positive
   //     printf("d=x=%I64d\n",x);
        while(l--)
        {
            scanf("%I64d",&y);
            ch = exp_mod(y,x,n); //C = y^d (mod n)
            printf("%c",ch);
        }
        printf("\n");
    }
    return 0;
}

posted @ 2010-09-12 19:12  菜到不得鸟  阅读(279)  评论(0)    收藏  举报