素数筛 唯一分解定理(质因子分解) 逆元 欧拉筛

素数筛
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值