FZU Problem 1759 Super A^B mod C(幂次循环节+快速乘法)

Problem Description

Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).

 Input

There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.
 Output

For each testcase, output an integer, denotes the result of A^B mod C.
 Sample Input

3 2 4
2 10 1000
 Sample Output

1
24
 Source

FZU 2009 Summer Training IV--Number Theory
噗,一版面都是我,最后发现欧拉函数爆了,丢。
指数循环节的问题,之前有用过,练习一下。
这里的B特别大,不能直接用,我们要实行降幂处理,有一个公式可以解决: 

这里写图片描述

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 35;
using namespace std;

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int eular(ll n)
{
    int ans=n;
    for(int i=2;i*i<=n;i++)
        if(n%i==0)
        {
            ans=ans/i*(i-1);
            while(n%i==0)n/=i;
        }
    if(n>1)ans=ans/n*(n-1);
    return ans;
}
ll fast_mod(ll a, ll b, ll c)
{
    ll res=1;
    while(b){
        if(b&1){
            res=res*a%c;
        }
        a=a*a%c;
        b>>=1;
    }
    return res;
}
ll getmod(char *st, ll c)
{
    ll res=0;
    int len=strlen(st);
    if(len<=16){
        //sscanf(st.c_str(), "%lld", &res);
        sscanf(st, "%lld", &res);
    }
    else{
        ll pi = eular(c);
        for(int i=0; i<len; ++i){
            res=(st[i]-'0'+res*10)%pi;
        }
        res=(res+pi)%pi;
    }
    return res;
}
ll A, C;
//string B;  //太慢
char B[1200100];
int main()
{
    //fin;
    while(~scanf("%lld%s%lld",&A,B,&C)){
        A%=C; //取模,结果相同,这里怕爆,所以先取模
        ll ret=getmod(B, C);
        //cout<<ret<<endl;
        cout<<fast_mod(A, ret, C)<<endl;
    }
    return 0;
}
新姿势:快速乘法,两个数太大相乘(可以取模)可能会直接爆掉,这时候我们就可以用快速乘法,乘的过程中取模就不怕被爆掉了。(和快速幂差不多,例如a*b,就是b个a相加,这时候就应该yy到了吧。)
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 35;
using namespace std;

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int eular(ll n)
{
    int ans=n;
    for(int i=2;i*i<=n;i++)
        if(n%i==0)
        {
            ans=ans/i*(i-1);
            while(n%i==0)n/=i;
        }
    if(n>1)ans=ans/n*(n-1);
    return ans;
}
ll fast_muti(ll a, ll b, ll c) //快速乘法
{
    ll res=0;
    a%=c;
    while(b){
        if(b&1){
            res=(res+a)%c;
        }
        a=(a+a)%c;
        b>>=1;
    }
    return res;
}

ll fast_mod(ll a, ll b, ll c)
{
    ll res=1;
    while(b){
        if(b&1){
            res=fast_muti(res,a,c);
        }
        a=fast_muti(a,a,c);
        b>>=1;
    }
    return res;
}
ll getmod(char *st, ll c)
{
    ll res=0;
    int len=strlen(st);
    if(len<=16){
        //sscanf(st.c_str(), "%lld", &res);
        sscanf(st, "%lld", &res);
    }
    else{
        ll pi = eular(c);
        for(int i=0; i<len; ++i){
            res=(st[i]-'0'+res*10)%pi;
        }
        res=(res+pi)%pi;
    }
    return res;
}
ll A, C;
//string B;
char B[1200100];
int main()
{
   // fin;
    while(~scanf("%lld%s%lld",&A,B,&C)){
        A%=C;
        ll ret=getmod(B, C);
        //cout<<ret<<endl;
        cout<<fast_mod(A, ret, C)<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值