您的位置:首页 > 其它

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

2016-10-03 01:50 363 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: