面向萌新的数论
_CuteRalsei_ · · 算法·理论
题单
以下内容,
P2522 [HAOI2011] Problem b
P2257 YY的GCD
P3704 [SDOI2017] 数字表格
大水题!!!
我们有预处理:
void func(int t){
int mul=1;
for(int d=1;d*d<=t;d++){
if(t%d)continue;
int x=d,y=t/d;
mul*=mupow(f[x],mu[t/x]);
mul%=mod;
if(x!=y)mul*=mupow(f[y],mu[t/y]);
mul%=mod;
}
fun[t]=mul;
}
void init_fun(){
for(int T=1;T<maxn;T++)func(T);//这里能不能优化?
pref[0]=1;
for(int i=1;i<maxn;i++){
pref[i]=pref[i-1]*fun[i];
pref[i]%=mod;
}
}
但是这是
于是我们优化。先枚举
void init_fun(){
for(int i=1;i<maxn;i++)fun[i]=1;
for(int d=1;d<maxn;d++){
for(int t=1;t*d<maxn;t++){
int T=t*d;
fun[T]*=mupow(f[d],mu[t]);
fun[T]%=mod;
}
}
pref[0]=1;
for(int i=1;i<maxn;i++){
pref[i]=pref[i-1]*fun[i];
pref[i]%=mod;
}
}
P1829 [国家集训队] Crash的数字表格 / JZPTAB
使用整除分块套整除分块可以做到
|-|-|-|-|-|-|-|-|-|-
|
整除分块套整除分块即可。但是为了保证复杂度,需要记录结果或预处理。
int F(int n){//f(n)=∑x=1^n n/x
if(calc[n])return f[n];//有记录
int ans=0;
for(int l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans+=(r-l+1)*(n/l);
}
f[n]=ans;
calc[n]=1;
return ans;
}
int solve(int n,int m){
int ans=0;
for(int l=1,r;l<=min(n,m);l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=(pre[r]-pre[l-1])*F(n/l)*F(m/l);
}
return ans;
}
P4980 【模板】Pólya 定理
AT_agc001_e [AGC001E] BBQ Hard
:::info[前置知识]{open}
有
问这些元素的排列有多少种。
| 答案显而易见: |
|---|
根据上面的公式,得到:
设
定义
其中
我们又知道,
所以
考虑 DP。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=200009,mod=1e9+7,V=2010;
int a[maxn],b[maxn],dp[V<<1|1][V<<1|1],fac[maxn];
void init(){
fac[0]=1;
for(int i=1;i<maxn;i++)fac[i]=fac[i-1]*i,fac[i]%=mod;
}
int qpow(int a,int b){
if(b==0)return 1;
if(b==1)return a;
int h=qpow(a,b>>1);
return h*h%mod*(b&1?a:1)%mod;
}
int inv(int x){return qpow(x,mod-2);}
int binom(int n,int m){return fac[n]*inv(fac[n-m])%mod*inv(fac[m])%mod;}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
int n;
init();
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
dp[V-a[i]][V-b[i]]++;
}
for(int i=1;i<(V<<1|1);i++){
for(int j=1;j<(V<<1|1);j++){
dp[i][j]+=dp[i-1][j]+dp[i][j-1];
dp[i][j]%=mod;
}
}
int ans=0,tmp=0;
for(int i=1;i<=n;i++){
ans+=dp[V+a[i]][V+b[i]];
ans%=mod;
}
for(int i=1;i<=n;i++){
tmp+=binom(a[i]*2+b[i]*2,a[i]*2);
tmp%=mod;
}
ans-=tmp;
ans=(ans%mod+mod)%mod;
cout<<ans*inv(2)%mod;
return 0;
}
CF785D Anton and School
设某一个括号 ( 前面(包括当前括号)有 (,后面有 ),那么贡献为:
其中
P4213 【模板】杜教筛
记:
则
SP3871 GCDEX
6 个月之后看自己代码居然看不懂了。
for(int i=1;i<=maxn;i++){
for(int j=i*2;j<=maxn;j+=i){
t[j]+=i*phi[j/i];
}
}
实际上,这个 t[] 是我们为了方便计算设置的。
SP19985 GCDEX2
同上题,如果想要用杜教筛计算,可以变形:
:::info[推导这个式子]{open}
:::
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
const int maxn=1e7;
bitset<maxn> isp;
int p[maxn],cnt=0,phi[maxn],prep[maxn];
unordered_map<int,int> S_mu,S_phi;
int S(int n){
if(n&1){
return (n+1)/2*n;
}
return n/2*(n+1);
}
void get(){
isp.set();
isp[1]=0;
prep[1]=1;
phi[1]=1;
for(int i=2;i<maxn;i++){
if(isp[i]){
p[++cnt]=i;
phi[i]=i-1;
}
for(int j=1,pr=p[j];j<=cnt&&i*pr<maxn;j++,pr=p[j]){
isp[i*pr]=0;
if(i%pr){
phi[i*pr]=phi[i]*phi[pr];
}//f(xy)=f(x)f(y),IF gcd(x,y)=1
else{
phi[i*pr]=phi[i]*pr;//p^k
break;
}
}
prep[i]=prep[i-1]+phi[i];
}
}
int pre_p(int n){
if(n<maxn)return prep[n];
if(S_phi[n])return S_phi[n];
int ans=S(n);
for(int l=2,r;l<=n;l=r+1){
r=n/(n/l);
ans-=(r-l+1)*pre_p(n/l);
}
S_phi[n]=ans;
return ans;
}
int SUM(int l,int r){
return S(r)-S(l-1);
}
int solve(int n){
int ans=0;
for(int l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans+=SUM(l,r)*(pre_p(n/l)-1ull);
}
return ans;
}
signed main(){
int T;
cin>>T;
get();
while(T--){
int n;
cin>>n;
cout<<solve(n)<<endl;
}
return 0;
}
P2424 约数和
#include<bits/stdc++.h>
using namespace std;
#define int long long
int solve(int n){
int cnt=0;
for(int l=1,r;l<=n;l=r+1){
if(n/l==0)break;
r=n/(n/l);
cnt+=(n/l)*(r-l+1)*(l+r)/2;
}
return cnt;
}
signed main(){
int l,r;
cin>>l>>r;
cout<<solve(r)-solve(l-1);
return 0;
}
P9611 [CERC2019] Zeldain Garden
#include<bits/stdc++.h>
using namespace std;
#define int long long
int solve(int n){
int cnt=0;
for(int l=1,r;l<=n;l=r+1){
if(n/l==0)break;
r=n/(n/l);
cnt+=(n/l)*(r-l+1);
}
return cnt;
}
signed main(){
int l,r;
cin>>l>>r;
cout<<solve(r)-solve(l-1);
return 0;
}
P1891 疯狂 LCM
如果
这两对之和为
下面是一个
#include<bits/extc++.h>
using namespace std;
const int maxn=1e6+10;
bitset<maxn> isp;
int p[maxn],cnt=0,phi[maxn];
void get(){
isp.set();
isp[1]=0;
for(int i=2;i<maxn;i++){
if(isp[i]){
p[++cnt]=i;
phi[i]=i-1;
}
for(int j=1,pr=p[j];j<=cnt&&i*pr<maxn;j++,pr=p[j]){
isp[i*pr]=0;
if(i%pr){
phi[i*pr]=phi[i]*phi[pr];
}//f(xy)=f(x)f(y),IF gcd(x,y)=1
else{
phi[i*pr]=phi[i]*pr;//p^k
break;
}
}
}
}
long long solve(int d){
if(d==1)return 1;
return 1ll*phi[d]*d/2;
}
#define endl '\n'
signed main(){
int T;
get();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
int n;
long long ans=0;
cin>>n;
for(int i=1;i*i<=n;i++){
if(n%i)continue;
int x=i,y=n/i;
ans+=solve(x);
if(y!=x)ans+=solve(y);
}
cout<<ans*n<<endl;
}
return 0;
}
:::
当然同 P3704 [SDOI2017] 数字表格,其实能预处理,做到
#include<bits/extc++.h>
using namespace std;
const int maxn=1e6+10;
bitset<maxn> isp;
int p[maxn],cnt=0,phi[maxn];
long long ans[maxn];
vector<int> inp;
void get(int M){
isp.set();
isp[1]=0;
for(int i=2;i<=M;i++){
if(isp[i]){
p[++cnt]=i;
phi[i]=i-1;
}
for(int j=1,pr=p[j];j<=cnt&&i*pr<=M;j++,pr=p[j]){
isp[i*pr]=0;
if(i%pr){
phi[i*pr]=phi[i]*phi[pr];
}//f(xy)=f(x)f(y),IF gcd(x,y)=1
else{
phi[i*pr]=phi[i]*pr;//p^k
break;
}
}
}
}
long long solve(int d){
if(d==1)return 1;
return 1ll*phi[d]*d/2;
}
#define endl '\n'
signed main(){
int T;
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>T;
int maxN=0;
while(T--){
int n;
cin>>n;
maxN=max(maxN,n);
inp.push_back(n);
}
get(maxN);
for(int i=1;i<=maxN;i++){
for(int j=1;i*j<=maxN;j++){
ans[i*j]+=solve(i);
}
}
for(auto n:inp){
cout<<ans[n]*n<<endl;
}
return 0;
}
:::
P3579 [POI 2014] PAN-Solar Panels
如果
那么最终式子就是:
对
:::info[为什么不用对
在块内,
于是判断条件就是
| 由于 |
|---|
| *** |
| ### P15765 [JAG 2025 Summer Camp #1] Sum of Floor(N/ij) |
| 注意到 |
于是我们只需要筛出
方法 1
考虑平衡复杂度。
考虑
对于单个
于是平衡一下:
复杂度
感谢 @Bingxiu2 提出复杂度问题。
#include<bits/extc++.h>
using namespace std;
const int maxn=3e7+10;
bool isp[maxn];
int p[maxn],cnt=0,d[maxn],Cnt[maxn],pred[maxn],B;//cnt:最小质因子次数
__gnu_pbds::gp_hash_table<int,long long> S_d;
inline void get(int V){
for(int i=0;i<V;i++)isp[i]=1;
isp[1]=0;
pred[1]=d[1]=1;
for(int i=2;i<V;i++){
if(isp[i]){
p[++cnt]=i;
d[i]=2;
Cnt[i]=1;
}
for(int j=1,pr=p[j];j<=cnt&&i*pr<V;j++,pr=p[j]){
isp[i*pr]=0;
if(i%pr){
d[i*pr]=d[i]*d[pr];
Cnt[i*pr]=1;
}//f(xy)=f(x)f(y),IF gcd(x,y)=1
else{
Cnt[i*pr]=Cnt[i]+1;
d[i*pr]=d[i]/(Cnt[i]+1)*(Cnt[i]+2);
break;
}
}
pred[i]=pred[i-1]+d[i];
}
}
#define endl '\n'
inline long long pre_d(int n){
if(n<B)return pred[n];
if(S_d[n])return S_d[n];
long long ans=0;
for(int l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans+=(n/l)*(r-l+1);
}
S_d[n]=ans;
return ans;
}
inline long long work(int n){
long long ans=0;
for(int l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans+=(n/l)*(pre_d(r)-pre_d(l-1));
}
return ans;
}
#define endl '\n'
namespace FAST_IO{
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#else
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#endif
inline long long read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=(x << 1) + (x << 3)+ch-48;ch=getchar();}
return x*f;
}//快读
inline void write(long long x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}//快输
}using namespace FAST_IO;
signed main(){
int T,T0;
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
T=read();
T0=T;
int MAXN=0;
vector<int> quer;
while(T--){
quer.push_back(read());
MAXN=max(MAXN,quer.back());
}
B=(int)sqrtl(1ll*T0*sqrt(MAXN)*MAXN)/3+10;
// putchar('B');
// write(B);
// putchar('\n');
// B=2e7;
get(B);
for(int i:quer){
write(work(i));
putchar('\n');
}
return 0;
}
方法 2
显然数据范围不允许线性筛,考虑使用杜教筛优化。
由于有
时间复杂度
但是……有点大炮打蚊子。 :::warning{open} 常数巨大! :::
#include<bits/extc++.h>
using namespace std;
const int maxn=1e7+10;
bitset<maxn> isp;
int p[maxn],cnt=0,d[maxn],Cnt[maxn],pred[maxn],mu[maxn],prem[maxn];//cnt:最小质因子次数
__gnu_pbds::gp_hash_table<int,long long> S_d,S_mu;
inline void get(){
isp.set();
isp[1]=0;
pred[1]=d[1]=mu[1]=prem[1]=1;
for(int i=2;i<maxn;i++){
if(isp[i]){
p[++cnt]=i;
d[i]=2;
Cnt[i]=1;
mu[i]=-1;
}
for(int j=1,pr=p[j];j<=cnt&&i*pr<maxn;j++,pr=p[j]){
isp[i*pr]=0;
if(i%pr){
d[i*pr]=d[i]*d[pr];
Cnt[i*pr]=1;
mu[i*pr]=mu[i]*mu[pr];
}//f(xy)=f(x)f(y),IF gcd(x,y)=1
else{
Cnt[i*pr]=Cnt[i]+1;
d[i*pr]=d[i]/(Cnt[i]+1)*(Cnt[i]+2);
mu[i*pr]=0;
break;
}
}
prem[i]=prem[i-1]+mu[i];
pred[i]=pred[i-1]+d[i];
}
}
#define endl '\n'
long long pre_m(int n){
if(n<maxn)return prem[n];
if(S_mu[n])return S_mu[n];
long long ans=1;
for(long long l=2,r;l<=n;l=r+1){
r=n/(n/l);
ans-=(r-l+1)*pre_m(n/l);
}
S_mu[n]=ans;
return ans;
}
inline long long pre_d(int n){
//f=d,g=μ,f*g=1
if(n<maxn)return pred[n];
if(S_d[n])return S_d[n];
long long ans=n;//pre_1(n)
for(long long l=2,r;l<=n;l=r+1){
r=n/(n/l);
ans-=(pre_m(r)-pre_m(l-1))*pre_d(n/l);
}
S_d[n]=ans;
return ans;
}
inline long long work(int n){
long long ans=0;
for(int l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans+=(n/l)*(pre_d(r)-pre_d(l-1));
}
return ans;
}
#define endl '\n'
signed main(){
int T;
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>T;
get();
// cout<<pre_m(1166161)<<endl;
while(T--){
int n;
cin>>n;
cout<<work(n)<<endl;
}
return 0;
}
生成函数
https://www.cnblogs.com/RabbitHu/p/9178645.html
P2000 拯救世界
kkk 的序列们:
对应
lzn 同理。
P4389 付公主的背包
考虑一个空间
生成函数为
是个人类都能想到改为:
不过不能直接模拟这个式子,不然当
修改一下得到:
// ...
int t[maxn];
#define endl '\n'
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,m,ma=0;
cin>>n>>m;
for(int i=1;i<=n;i++){
int x;
cin>>x;
ma=max(ma,x);
t[x]++;
}
poly f(m+1);
f[0]=0;
for(int i=1;i<=ma;i++){//枚举是 v[i] 的数
if(!t[i])continue;
for(int j=i;j<=m;j+=i){
f[j]=(f[j]+inv(j/i)*(long long)t[i])%mod;
}
}
f=exp(f);
for(int i=1;i<=m;i++)cout<<f[i]<<endl;
return 0;
}
P3934 [Ynoi Easy Round 2016] 炸脖龙 I
记
P6810 「MCOI-02」Convex Hull 凸包
:::info[code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=2e6+10;
bitset<maxn> isp;
int p[maxn],tau[maxn],ans[2][maxn],n,m,cnt,Cnt[maxn],mod;
void sieve(int V){
for(int i=0;i<=V;i++)isp[i]=1;
isp[1]=0;
tau[1]=1;
for(int i=2;i<=V;i++){
if(isp[i]){
p[++cnt]=i;
tau[i]=2;
Cnt[i]=1;
}
for(int j=1,pr=p[j];j<=cnt&&i*pr<=V;j++,pr=p[j]){
isp[i*pr]=0;
if(i%pr){
tau[i*pr]=tau[i]*tau[pr];
Cnt[i*pr]=1;
}//f(xy)=f(x)f(y),IF gcd(x,y)=1
else{
Cnt[i*pr]=Cnt[i]+1;
tau[i*pr]=tau[i]/(Cnt[i]+1)*(Cnt[i]+2);
break;
}
}
}
}
int getans(int d,int lim){
int tot=0;
for(int i=d;i<=lim;i+=d){
tot+=tau[i];
tot%=mod;
}
return tot;
}
void init(){
for(int d=1;d<=n;d++)ans[0][d]=getans(d,n);
for(int d=1;d<=m;d++)ans[1][d]=getans(d,m);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m>>mod;
sieve(max(n,m));
init();
int tot=0;
for(int d=1;d<=min(n,m);d++){
tot+=ans[0][d]*ans[1][d]%mod;
tot%=mod;
}
cout<<tot;
return 0;
}
:::
后面两个求和随便预处理。
P6091 【模板】原根
最小原根
又知道
P3846 【模板】BSGS / [TJOI2007] 可爱的质数
类根号分治状物。
设解的形式是
转化为:
也就是
很水。
#include<bits/stdc++.h>
using namespace std;
#define int long long
unordered_map <int, int> vis;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
int a, b, p;
cin >> p >> a >> b;
int M = ceil(sqrt(p)) + 1, powa = 1, powu = 1, powm = 1;
for(int v = 0; v <= M; v++, powa *= a, powa %= p){
vis[b * powa % p] = v;
if(v < M)powm *= a, powm %= p;
}
int ans = LLONG_MAX;
for(int u = 0; u <= M; u++, powu *= powm, powu %= p){
if(vis.count(powu) && u * M >= vis[powu])ans = min(ans, u * M - vis[powu]);
}
if(ans == LLONG_MAX)cout << "no solution";
else cout << ans;
return 0;
}
P3911 最小公倍数之和
设