题目背景
这是一道模板题
题目描述
给定n,p求1~n中所有整数在模p意义下的乘法逆元。
输入输出格式
输入格式:
一行n,p
输出格式:
n行,第i行表示i在模p意义下的逆元。
输入输出样例
输入样例#1:
10 13
输出样例#1:
1 7 9 10 8 11 2 5 3 4
说明
1≤n≤3×106,n<p<20000528
输入保证 p 为质数。
我们有三种办法求逆元
由欧拉定理可知
当gcd(a,n)==1 时 我们有 Aφ(n-1)≡ 1(mod n) ;
所以 我们有 A*Aφ(n-2) ≡ 1(mod n)
所以Aφ(n-2) 就是A关于mod n的逆元
1 /* 2 p为素数时 可用费马小定理 3 longlong*longlong 要慢一点 66分 4 */ 5 #include <cctype> 6 #include <cstdio> 7 8 typedef long long LL; 9 10 int n,p; 11 12 inline LL quick_pow(LL a,int k) { 13 LL ret=1; 14 while(k) { 15 if(k&1) ret=(ret*a)%p; 16 k>>=1; 17 a=(a*a)%p; 18 } 19 return ret; 20 } 21 22 int hh() { 23 scanf("%d%d",&n,&p); 24 printf("1\n"); 25 for(int i=2;i<=n;++i) { 26 LL t=quick_pow(i,p-2); 27 printf("%d\n",(t+p)%p); 28 } 29 return 0; 30 } 31 32 int sb=hh(); 33 int main(int argc,char**argv) {;}