题目概述
求 xx⋮xn21 mod p x 1 x 2 ⋮ x n m o d p 。
解题报告
欧拉定理: aφ(p)≡1(mod p)⇔ab≡ab mod φ(p)(mod p) a φ ( p ) ≡ 1 ( m o d p ) ⇔ a b ≡ a b m o d φ ( p ) ( m o d p ) ,可以用来降幂,但是只适用于 gcd(a,p)=1 g c d ( a , p ) = 1 。
实际上有扩展欧拉定理:
- gcd(a,p)=1 g c d ( a , p ) = 1 : ab≡ab mod φ(p)(mod p) a b ≡ a b m o d φ ( p ) ( m o d p )
-
gcd(a,p)>1
g
c
d
(
a
,
p
)
>
1
- b<φ(p) b < φ ( p ) : ab≡ab(mod p) a b ≡ a b ( m o d p )
- b≥φ(p) b ≥ φ ( p ) : ab≡ab mod φ(p)+φ(p)(mod p) a b ≡ a b m o d φ ( p ) + φ ( p ) ( m o d p )
2018.7.1UPD:之前写成 b<p b < p 和 b≥p b ≥ p 了……很抱歉误导了读者老爷们Orz……
证明?我不会啊!然后就可以实现降幂了,这道题直接暴搞就行了QAQ。
示例程序
#include<cstdio>
#include<cctype>
#include<algorithm>
#define fr first
#define sc second
#define mp make_pair
using namespace std;
typedef long long LL;typedef pair<int,bool> data;
const int maxn=1000000,maxm=1000000;
int te,m,n,x[maxn+5];
int p[maxm+5],phi[maxm+5];bool pri[maxm+5];
#define Eoln(x) ((x)==10||(x)==13||(x)==EOF)
inline char readc()
{
static char buf[100000],*l=buf,*r=buf;
if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
if (l==r) return EOF;return *l++;
}
inline int readi(int &x)
{
int tot=0,f=1;char ch=readc(),lst='+';
while (!isdigit(ch)) {if (ch==EOF) return EOF;lst=ch;ch=readc();}
if (lst=='-') f=-f;
while (isdigit(ch)) tot=(tot<<3)+(tot<<1)+ch-48,ch=readc();
return x=tot*f,Eoln(ch);
}
void Make()
{
pri[1]=true;phi[1]=1;
for (int i=2;i<=m;i++)
{
if (!pri[i]) p[++p[0]]=i,phi[i]=i-1;
for (int j=1,t;j<=p[0]&&(t=p[j]*i)<=m;j++)
{
if (i%p[j]) phi[t]=phi[i]*phi[p[j]],pri[t]=true; else
{phi[t]=phi[i]*p[j];pri[t]=true;break;}
}
}
}
int gcd(int a,int b) {if (!b) return a;return gcd(b,a%b);}
inline data Pow(LL w,int b,int MOD)
{
LL s=1;bool fl=true;
while (b)
{
if (b&1) {if ((s*=w)>=MOD) fl=false;s%=MOD;}
b>>=1;if (b) {if ((w*=w)>=MOD) fl=false;w%=MOD;}
}
return mp(s,fl);
}
data Solve(int st,int MOD)
{
if (MOD==1) return mp(0,false);if (st==n) return mp(x[st]%MOD,x[st]<MOD);
data b=Solve(st+1,phi[MOD]);if (gcd(x[st],MOD)==1) return Pow(x[st],b.fr,MOD);
if (!b.sc) b.fr+=phi[MOD];return Pow(x[st],b.fr,MOD);
}
int main()
{
freopen("program.in","r",stdin);
freopen("program.out","w",stdout);
readi(te);readi(m);Make();
while (te--)
{
readi(n);for (int i=1;i<=n;i++) readi(x[i]);
printf("%d\n",Solve(1,m).fr);
}
return 0;
}