求phi(敲黑板)
#include <iostream>
#include <cstdio>
using namespace std;
int a,b,m,temp,phi,ans=1;
bool flag;
int main()
{
int i;
char c;
scanf("%d%d",&a,&m);
temp=phi=m;
for (i=2;i*i<=m;++i) {
if (temp%i==0) {
phi=phi-phi/i;
while (temp%i==0)
temp/=i;
}
}
if (temp>1)
phi=phi-phi/temp;
while (!isdigit(c=getchar()));
for (;isdigit(c);c=getchar())
{
b=b*10+c-'0';
if (b>=phi)
{
flag=true;
b%=phi;
}
}
if (flag)
b+=phi;
for (i=20;i>=0;--i) {
ans=1ll*ans*ans%m;
if (b&(1<<i)) {
ans=1ll*ans*a%m;
}
}
cout<<ans;
return 0;
}
求欧拉函数!!!
temp=phi=m;
for (i=2;i*i<=m;++i) {
if (temp%i==0) {
phi=phi-phi/i;
while (temp%i==0)
temp/=i;
}
}
为啥呢
欧拉函数通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数。φ(1)=1
φ(x)=(x-x/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn)依次套进去 把当前的因子除净