codeforces 17D. Notepad 欧拉函数降幂

来源:互联网 发布:北京金和网络股份员工 编辑:程序博客网 时间:2024/06/01 19:04

D. Notepad
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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 < 101061 ≤ n < 101061 ≤ 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 4 numbers can be written on the first page.


题意:计算(b-1)*b^(n-1)%c,数据范围 2 ≤ b < 101061 ≤ n < 101061 ≤ c ≤ 109


给出两个公式:

a^b%c=a^(b%phi(c)). 欧拉函数降幂,要求a与c互质
a^b %c= a^(b%phi(c)+phi(c)) %c (b>=phi(c),不大于时直接二分幂)   扩展的欧拉函数用于降幂,无互质要求


CODE:

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N = 1e6+10;LL c;char b[N];  ///bchar s[N];  ///nLL 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(void){    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;}




0 0
原创粉丝点击