欧拉降幂模板

 

 

欧拉降幂公式:

欧æéå¹å¬å¼

第一个是a和p互质的时候,后两个是a和p不互质的时候。

来一道裸题,稍微有点变化,两层的。

一般情况下数据比较大的时候,a和b都定义为字符数组型。

 

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
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
char b[1000006];
ll a,c;
ll quickpow(ll x,ll y,ll z)
{
    ll ans=1;
    x%=z;
    while(y)
    {
        if(y&1)
            ans=ans*x%z;
        x=x*x%z;
        y>>=1;
    }
    return ans;
}
ll phi(ll n)
{
    ll i,rea=n;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            rea=rea-rea/i;
            while(n%i==0)
                n/=i;
         }
    }
    if(n>1)
        rea=rea-rea/n;
    return rea;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>a>>b>>c)
    {
        ll len=strlen(b);
        ll p=phi(c);
        ll ans=0;
        for(ll i=0;i<len;i++)
        {
            ans=(ans*10+b[i]-'0')%p;
        }
        ans+=p;
        cout<<quickpow(a,ans,c)<<endl;
    }
   // cout<<quickpow(2,410,1000)<<endl;
    return 0;
}

题目点这里

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<stack>
using namespace std;
#define maxn 100005
typedef long long ll;
const int p=1e9+7;
ll qsm(ll a,ll b,ll p){
    ll res=1;
    res%=p;
    while(b){
        if(b&1) res=res*a%p;
        a=(a*a)%p;
        b>>=1;
    }
    return res;
}
ll ph(ll n){
    ll ans=n;
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            ans=ans*(i-1)/i;
            while(n%i==0) n/=i;
        }
    }
    if(ans>1) ans=ans*(n-1)/n;
    return ans;
}
ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}
int main(){
    ll a,b,c,d;
    while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF)
    {
        ll mi=ph(p);
        if(gcd(b,p)!=1)
        {
            if(c>=mi) 
            c=(c%mi)+mi;
        }
        else 
        c=c%mi;
        d=qsm(b,c,p);
        printf("%lld\n",qsm(a,d%(p),p));
    }
    return 0;
}

今天做到一道:

Nick is attracted by everything unconventional. He doesn't like decimal number system any more, and he decided to study other number systems. A number system with base b caught his attention. Before he starts studying it, he wants to write in his notepad all the numbers of length n without leading zeros in this number system. Each page in Nick's notepad has enough space for c numbers exactly. Nick writes every suitable number only once, starting with the first clean page and leaving no clean spaces. Nick never writes number 0 as he has unpleasant memories about zero divide.

Would you help Nick find out how many numbers will be written on the last page.

Input

The only input line contains three space-separated integers bn and c (2 ≤ b < 10106, 1 ≤ n < 10106, 1 ≤ c ≤ 109). You may consider that Nick has infinite patience, endless amount of paper and representations of digits as characters. The numbers doesn't contain leading zeros.

Output

In the only line output the amount of numbers written on the same page as the last number.

Examples

Input

2 3 3

Output

1

Input

2 3 4

Output

4

Note

In both samples there are exactly 4 numbers of length 3 in binary number system. In the first sample Nick writes 3 numbers on the first page and 1 on the second page. In the second sample all the 4numbers can be written on the first page.

题意:计算(b-1)*b^(n-1)%c

从数据范围来看,肯定要用到欧拉降幂。

根据上面的三个公式变换即可,用扩展的欧拉降幂后面两个公式。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = 1e6+10;
LL c;
char b[N];  //b
char s[N];  //n
LL euler_phi(LL n)
{         //计算单个phi函数
    LL m =(int)sqrt(n+0.5);
    LL ans = n;
    for(LL i = 2;i <= m;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 Pow(LL a,LL b,LL mod){   //二分幂
    LL ret = 1;
    LL A = a;
    while(b){
        if(b&1) ret = ret*A%mod;
        A = A*A%mod;
        b >>= 1;
    }
    return ret;
}
int main()
{
    scanf("%s%s%I64d",b,s,&c);
    int lenb = strlen(b);
    int lens = strlen(s);
    LL tb=0,tb1=0;    //b和b-1
    for(int i = 0;i < lenb;i++){     //计算b
        tb = (tb*10+b[i]-'0')%c;
    }
    tb1=(tb-1+c)%c;  //计算b-1
    LL phic=euler_phi(c);
    for(int i=lens-1;i>=0;i--){  //n-1
        if(s[i]==0) 
            s[i]='9';
        else{
            s[i]--;
            break;
        }
    }
    bool flag=false; //标记n是否大于等于phic
    LL tn=0;         //n
    for(int i=0;i<lens;i++)
    {     //计算n
        if(tn>=phic) 
            flag=true;
        if(flag) 
        tn=(tn*10+s[i]-'0')%phic;
        else     
        tn=tn*10+s[i]-'0';
    }
    if(flag) 
    tn+=phic;             //n大于等于phic的时候才加
    LL ans=Pow(tb,tn,c);
    ans=ans*tb1%c;
    if(ans==0)
    printf("%I64d\n",c);
    else
    printf("%I64d\n",ans);
    return 0;
}

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值