 /**//*
就是求(b-1)*b^(n-1) % c
其中2<=b<=10^10^6 1<=n<=10^10^6,1<=c<=10^9
用Java高精度,超时了
看了别人的代码,神奇丫

b % c 其中b是大整数,可以从高位往低位逐位搞 r = (r*10+b[i])%c
更神奇的是,a^b%c一样也可以用上面那样子搞
但是就不是r*10而是r^10, r = (r^10*(a^b[i]))%c
a^b[i]可以预处理,即a^0,a^1, ,a^9

有用公式
a^b % c = a^b' %c
其中b' = b>= p?b%p+p: b%p p = phi(c)
这个公式成立不需要(a,c)=1!!!
*/
#include<iostream>
#include<cstring>
#include<map>
#include<algorithm>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdlib>
#include<vector>
#include<cstdio>
#include<set>
#include<list>
#include<numeric>
#include<cassert>
#include<ctime>
#include<bitset>

using namespace std;

const int MAXN = 1000010;

char b[MAXN], n[MAXN];
long long p[10];

int main()
  {
#ifndef ONLINE_JUDGE
// freopen("in","r",stdin);
#endif
 for (long long c; ~scanf("%s%s%I64d", b, n, &c); ) {
//(b-1)*b^(n-1) % c
int len = strlen(b);
long long r = 0, left;
 for (int i = 0; i < len ; i ++) {//r = b%c
r = (r*10 + b[i]-'0') % c;
}
left = (r+c-1) % c;
len = strlen(n);
 for (int i = len - 1; i >= 0 ; i-- ) {
 if (n[i] == '0') {
n[i] = '9';
 }else {
n[i] --;
break;
}
}
//p[i] = b^i % c;
p[0] = 1;
 for(int i = 1 ; i < 10 ; i++) {
p[i] = p[i-1] * r % c;
}
r = 1;
 for (int i = 0; i < len ; i ++) {
long long t = r;
 for (int j = 1 ;j < 10 ; j++) {//b^(l*10)
r = r * t % c;
}
r = r*p[n[i]-'0'] % c;
}

r = left * r % c;
printf("%I64d\n", r == 0 ? c : r);
}
return 0;
}
|
|
常用链接
随笔分类
Links
搜索
最新评论

|
|