以下内容, 表示质数集合, 表示 , 为艾弗森括号。
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;
}
}但是这是 ,会 TLE。
于是我们优化。先枚举 ,再枚举它的倍数 ,这样就是 。可以预处理逆元做到 。
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
P3327 [SDOI2015] 约数个数和
第一步的感性理解,以 为例。
| 因子 | |||||||||
|---|---|---|---|---|---|---|---|---|---|
整除分块套整除分块即可。但是为了保证复杂度,需要记录结果或预处理。
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
前置知识
有 种元素,第 种元素有 个,且这 个元素相同。
问这些元素的排列有多少种。
答案显而易见:。
根据上面的公式,得到:
设 为没有 限制的答案。
定义 为只向上走合向右走,从 点走到 点的方案数。
其中 。
我们又知道,,其中 均为任意数。
所以 。
考虑 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
同上题,如果想要用杜教筛计算,可以变形:
推导这个式子
#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
如果 存在 的倍数,那么满足 。
那么最终式子就是:
对 整除分块即可。
为什么不用对 整除分块
在块内, 的值是一定的,设为 。
于是判断条件就是 。
由于 是 中最大的数,且是让 最小的数,因此我们只需要判断 即可。
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
常数巨大!
#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
记