#include<iostream>#include<algorithm>usingnamespace std;boolis_prime(int x){if(x <2)returnfalse;for(int i =2; i <= x / i; i ++)if(x % i ==0)returnfalse;returntrue;}intmain(){int n;
cin >> n;while(n --){int x;
cin >> x;if(is_prime(x))puts("Yes");elseputs("No");}return0;}
线性筛
#include<iostream>#include<algorithm>usingnamespace std;constint N=1000010;int primes[N], cnt;bool st[N];voidget_primes(int n){for(int i =2; i <= n; i ++){if(!st[i]) primes[cnt ++]= i;for(int j =0; primes[j]<= n / i; j ++){
st[primes[j]* i]=true;if(i % primes[j]==0)break;}}}intmain(){int n;
cin >> n;get_primes(n);
cout << cnt << endl;return0;}
#include<iostream>#include<algorithm>usingnamespace std;typedeflonglong LL;
LL qmi(int a,int b,int p){
LL res =1% p;while(b){if(b &1) res = res * a % p;
a = a *(LL)a % p;
b >>=1;}return res;}intmain(){int n;scanf("%d",&n);while(n --){int a, b, p;scanf("%d%d%d",&a,&b,&p);printf("%lld\n",qmi(a, b, p));}return0;}
求组合数一(a,b,1000)
代码(杨辉三角
#include<iostream>#include<algorithm>usingnamespace std;constint N =2010, mod =1e9+7;int c[N][N];voidinit(){for(int i =0; i < N; i ++)for(int j =0; j <= i; j ++)if(!j) c[i][j]=1;else c[i][j]=(c[i -1][j]+ c[i -1][j -1])% mod;}intmain(){int n;init();scanf("%d",&n);while(n --){int a, b;scanf("%d%d",&a,&b);printf("%d\n", c[a][b]);}return0;}
求组合数二(a,b,10000)
代码
#include<iostream>#include<algorithm>usingnamespace std;typedeflonglong LL;constint N =100010, mod =1e9+7;int fact[N], infact[N];intqmi(int a,int k,int p){int res =1;while(k){if(k &1) res =(LL)res * a % p;
a =(LL)a * a % p;
k >>=1;}return res;}intmain(){
fact[0]= infact[0]=1;for(int i =1; i < N; i ++){
fact[i]=(LL)fact[i -1]* i % mod;
infact[i]=(LL)infact[i -1]*qmi(i, mod -2, mod)% mod;}int n;scanf("%d",&n);while(n --){int a, b;scanf("%d%d",&a,&b);printf("%d\n",(LL)fact[a]* infact[b]% mod * infact[a - b]% mod);}return0;}
求组合数三(a,b超级大)
代码:卢卡斯定理
#include<iostream>#include<algorithm>usingnamespace std;typedeflonglong LL;intqmi(int a,int k,int p){int res =1;while(k){if(k &1) res =(LL)res * a % p;
a =(LL)a * a % p;
k >>=1;}return res;}intC(int a,int b,int p){if(b > a)return0;int res =1;for(int i =1, j = a; i <= b; i ++, j --){
res =(LL)res * j % p;
res =(LL)res *qmi(i, p -2, p)% p;}return res;}intlucas(LL a, LL b,int p){if(a < p && b < p)returnC(a, b, p);return(LL)C(a % p, b % p, p)*lucas(a / p, b / p, p)% p;}intmain(){int n;
cin >> n;while(n --){
LL a, b;int p;
cin >> a >> b >> p;
cout <<lucas(a, b, p)<< endl;}return0;}
posted on
2020-08-14 10:24谁是凶手1703
阅读(79)
评论(0)
收藏举报