您的位置:首页 > 其它

欧拉降幂公式—— BZOJ 3884 && FZU 1759

2017-08-08 17:58 477 查看


C - Super A^B mod C

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<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int INF_INT  = 0x3f3f3f3f;
const ll INF_LL  = 1e18;
const int MOD=1e9 + 7;
const int N = 100005;

//int phi
;
//vector<int> prime;
//void init()
//{
//    phi[1] = 1;
//    for(int i=2;i<N;i++)
//    {
//        if(phi[i]==0)
//        {
//            prime.push_back(i);
//            phi[i] = i-1;
//        }
//        for(int j=0;j<prime.size()&&prime[j]*i<N;j++)
//        {
//            if(i%prime[j]==0)
//            {
//                phi[i*prime[j]] = phi[i]*prime[j];
//                break;
//            }
//            else
//            {
//                phi[i*prime[j]] = phi[i]*(prime[j]-1);
//            }
//        }
//    }
//}

//计算欧拉函数O(sqrt(n))
int Phi(int x)
{
int i,re=x;
for(i=2;i*i<=x;i++)
if(x%i==0)
{
re/=i;re*=i-1;
while(x%i==0)
x/=i;
}
if(x^1) re/=x,re*=x-1;
return re;
}

ll Quick_Power(ll x,ll y,ll p)
{
ll re=1;
while(y)
{
if(y&1) (re*=x)%=p;
(x*=x)%=p; y>>=1;
}
return re;
}

int Solve(int p)
{
if(p==1) return 0;
int phi_p = Phi(p);
return Quick_Power(2,Solve(phi_p)+phi_p,p);
}

int T,n,a,c;
string b;

int main()
{
ios_base::sync_with_stdio(false);
//    freopen("data.txt","r",stdin);

while(cin>>a>>b>>c)
{
int phi_c = Phi(c);
ll sum = 0;
for(int i=0;i<b.size();i++)
{
sum = (sum*10+b[i]-'0')%(phi_c);
}

cout <<Quick_Power(a,sum+phi_c,c)<<endl;
}

}


BZOJ 3884 上帝与集合的正确用法

根据一些书上的记载,上帝的一次失败的创世经历是这样的:
第一天,   上帝创造了一个世界的基本元素,称做“元”。
第二天,   上帝创造了一个新的元素,称作“α”。“α”被定义为“元”构成的集合。容易发现,一共有两种不同的“α”。
第三天,   上帝又创造了一个新的元素,称作“β”。“β”被定义为“α”构成的集合。容易发现,一共有四种不同的“β”。
第四天,   上帝创造了新的元素“γ”,“γ”被定义为“β”的集合。显然,一共会有16种不同的“γ”。
如果按照这样下去,上帝创造的第四种元素将会有65536种,第五种元素将会有2^65536种。这将会是一个天文数字。
然而,上帝并没有预料到元素种类数的增长是如此的迅速。他想要让世界的元素丰富起来,因此,日复一日,年复一年,他重复地创造着新的元素……
然而不久,当上帝创造出最后一种元素“θ”时,他发现这世界的元素实在是太多了,以致于世界的容量不足,无法承受。因此在这一天,上帝毁灭了世界。
至今,上帝仍记得那次失败的创世经历,现在他想问问你,他最后一次创造的元素“θ”一共有多少种?
上帝觉得这个数字可能过于巨大而无法表示出来,因此你只需要回答这个数对p取模后的值即可。
你可以认为上帝从“α”到“θ”一共创造了10^9次元素,或10^18次,或者干脆∞次。


Input

接下来T行,每行一个正整数p,代表你需要取模的值


Output

T行,每行一个正整数,为答案对p取模后的值


Sample Input

3
2
3
6


Sample Output

0
1
4


Hint

对于100%的数据,T<=1000,p<=10^7


利用递归形式求解


出题人题解

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int INF_INT  = 0x3f3f3f3f;
const ll INF_LL  = 1e18;
const int MOD=1e9 + 7;
const int N = 100005;

//int phi
;
//vector<int> prime;
//void init()
//{
//    phi[1] = 1;
//    for(int i=2;i<N;i++)
//    {
//        if(phi[i]==0)
//        {
//            prime.push_back(i);
//            phi[i] = i-1;
//        }
//        for(int j=0;j<prime.size()&&prime[j]*i<N;j++)
//        {
//            if(i%prime[j]==0)
//            {
//                phi[i*prime[j]] = phi[i]*prime[j];
//                break;
//            }
//            else
//            {
//                phi[i*prime[j]] = phi[i]*(prime[j]-1);
//            }
//        }
//    }
//}

//计算欧拉函数O(sqrt(n))
int Phi(int x)
{
int i,re=x;
for(i=2;i*i<=x;i++)
if(x%i==0)
{
re/=i;re*=i-1;
while(x%i==0)
x/=i;
}
if(x^1) re/=x,re*=x-1;
return re;
}

ll Quick_Power(ll x,ll y,ll p)
{
ll re=1;
while(y)
{
if(y&1) (re*=x)%=p;
(x*=x)%=p; y>>=1;
}
return re;
}

int Solve(int p)
{
if(p==1) return 0;
int phi_p = Phi(p);
return Quick_Power(2,Solve(phi_p)+phi_p,p);
}

int T,n;
string s;

int main()
{
ios_base::sync_with_stdio(false);
//    freopen("data.txt","r",stdin);
cin>>T;
while(T--)
{
cin >> n;
cout <<Solve(n)<<endl;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: