素数筛
n,q<=1e6
如果暴力求素数 根号n=1000 1E9超时
nlogn预处理素数 O(1*q)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int maxn=1e6+5;
ll q,a[maxn],f,ans,k,n;
string s,t;
bool prime[maxn];
void init(){
memset(prime,true,sizeof prime);
prime[1]=0;
for(int i=2;i<=maxn;i++){
if(prime[i]==0)
continue;
for(int j=2*i;j<=maxn;j+=i){ //2:4 6 3:6 9 6重复标记 O(n*logn) 每个数的因子有logn个
prime[j]=0;
}
}
}
int main(){
init();
cin>>q;
while(q--){
cin>>n;
if(prime[n])
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return 0;
}
C(n,2)+…+C(n,n) n<=1e6 快速幂求组合数 =2^n-(n+1) logn
唯一分解定理
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int maxn=1e6+5;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
ll q,a[maxn],f,ans,k,n,cnt,c,maxx;
string s,t;
bool prime[maxn];
//1e14 素因子 17280个 求 n=a*b^p 的最大p 2e5*log1e14=2e5*50=1e7
int main(){
cin>>n;
for(ll i=2;i*i<=n;i++){
if(n%i==0){
cnt=0;
while(n%i==0){
n=n/i;
cnt++;
}
a[c++]=cnt;
}
}
if(n!=1){
n=1;
a[c++]=1; //n^1
}
for(int i=0;i<c;i++){
maxx=max(maxx,a[i]);
}
cout<<maxx<<endl;
return 0;
}
逆元问题
n/m %mod =q
n*k%mod=q
/m *k有相同的结果 k为m的逆元
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int maxn=1e6+5;
const ll mod=1e9+7;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
ll q,k,n,a[maxn],f,ans,cnt,maxx;
string s,t;
bool prime[maxn];
ll quick_pow(ll a,ll n){
ll res=1;
while(n){
if(n&1){
res=res*a%mod;
}
a=a*a%mod;
n>>=1;
}
return res;
}
ll inv_mod(ll a,ll p){
return quick_pow(a,p);
}
int main(){
cin>>k>>n;
cout<<quick_pow(k,n)<<endl;
cout<<inv_mod(k,mod-2)<<endl;
return 0;
}