X-man

导航

快速幂

http://ch.vijos.org/Contest/%E8%BE%BD%E5%AE%81%E7%9C%81%E9%98%9F%E4%BA%92%E6%B5%8B%20Weak%201/Problem/Show/%E5%BF%AB%E9%80%9F%E5%B9%82

#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
string s;
int mod;
long long multy(long long q, long long n)
{
    long long cnt = n;
    long long base = q;
    long long ret = 1;
    while(cnt > 0)
    {
        if(cnt & 1)
            ret = (long long)ret*base%mod;
        cnt = cnt >> 1;
        base = (long long)base*base%mod;
    }
    return ret;
}
int main()
{
    int y;
    long long ans;
    while(cin>>s)
    {
        scanf("%d",&mod);
        y=0;
        for(int i=0;i<s.size()&&mod!=1;i++)
        {
            y=(y*10+s[i]-'0')%(mod-1);
        }
        ans=multy(2,y);
        printf("%lld\n",ans%mod);
    }
    return 0;
}

 

posted on 2013-05-19 19:16  雨钝风轻  阅读(203)  评论(0编辑  收藏  举报