容斥
容斥 (Big|igcuplimits_{i=1}^nS_iBig|=sumlimits_{m=1}^n(-1)^{m-1} sumlimits_{a_i<a_{i+1}}Big|igcaplimits_{i=1}^mS_{a_i}Big|)
不定方程非负整数解计数
不定方程(sum_{i=1}^n x_i=m)和(n)个限制条件(x_ile b_i)
没有限制时 其非负整数解为(C_{m+n-1}^{m-1}) 相当于有(m)个球分给(n)个盒子 允许有空盒子
容斥:
1. 全集$cup$:不定方程$sum_{i=1}^n x_i=m$的非负整数解 有$C_{m+n-1}^{m-1}$个
- 元素:变量(x_i)
- 属性:(x_i)的属性即其限制条件
目标:满足对应关系时集合大小(Big|igcap_{i=1}^n S_iBig|)
(Big|igcap_{i=1}^n S_iBig|=Big|igcupBig|-Big|igcup_{i=1}^noverline{S_i}) 即变为对于一些
HAOI2008硬币购物
即可转化为求方程(sum_{i-1}^4 C_ix_i=S,x_ile D_i)的非负整数解的个数
最后求解(sumlimits_{i=1}^4C_ix_i=S-sumlimits_{i=1}^kC_{a_i}(D_{a_i}+1))
int main(){
f[0]=1;
for(int i=1;i<=4;++i){
rd(a[i]);
for(int v=a[i];v<M;++v) f[v]+=f[v-a[i]];
}
int T;rd(T);
while(T--){
rd(d[1]),rd(d[2]),rd(d[3]),rd(d[4]),rd(s);
ans=0;
for(int i=1,m,bit;i<(1<<4);++i){//二进制枚举集合
m=s,bit=0;
for(int j=1;j<=4;++j)
if((i>>(j-1))&1) m-=(d[j]+1)*a[j],++bit;
if(m>=0) ans+=(bit%2*2-1)*f[m];//奇加偶减
}
printf("%lld
",f[s]-ans);
}
}
以前写的是dfs版
void dfs(int nw,int mon,int fla){
if(mon<0) return;
if(nw>4) {ans+=f[mon]*fla;return;}
dfs(nw+1,mon,fla);
dfs(nw+1,mon-a[nw]*(d[nw]+1),-fla);
}
int main(){
f[0]=1;
for(int i=1;i<=4;++i){
rd(a[i]);
for(int v=a[i];v<M;++v) f[v]+=f[v-a[i]];
}
rd(T);
while(T--){
for(int i=1;i<=4;++i) rd(d[i]);
rd(m);
ans=0;
dfs(1,m,1);
printf("%lld
",ans);
}
return 0;
}
(n)的错位排列数为
(D_n=n!-n!sumlimits_{k=1}^nfrac{(-1)^{k-1}}{k!}=n!sumlimits_{k=0}^nfrac{(-1)^k}{k!})
组合
错位排序 即信封装错问题 递推式:(f(n)=(n-1)(f(n-1)+f(n-2)))
圆排列:(Q_n^r=frac{A_n^r}{r}=frac{n!}{r*(n-r)!})
luogu1822魔法指纹
分块打表真的妙啊!!!
同样思想:BZOJ3798特殊的质数
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int base=1000000;
int table[]={0,2045,1044,750,357,316,316,358,749,1044,1474,866,1044,303,220,154,115,84,125,346,927,378,558,750,221,204,154,118,134,146,382,110,270,303,357,176,204,168,128,89,75,85,126,298,221,316,176,220,168,88,69,69,88,168,220,176,316,221,298,126,85,75,89,128,168,204,176,357,303,270,111,382,145,134,118,154,204,222,749,558,378,927,347,124,84,115,154,220,303,1044,866,2022,754,224,71,86,115,168,298,558,1474,626,558,118,81,51,33,19,30,316,1461,866,1044,303,220,154,115,84,125,346,927,110,558,94,221,96,77,48,32,26,60,74,270,116,66,95,204,118,83,55,29,50,126,118,77,50,76,113,168,57,43,44,88,108,79,40,41,44,91,98,85,41,89,69,81,34,23,30,21,80,67,136,145,74,64,51,21,22,24,35,53,489,347,75,61,51,24,13,28,172,79,1143,754,47,45,51,36,15,20,229,924,240,240,298,118,69,51,28,43,63,585,866,328,303,113,77,51,35,25,62,375,378,558,750,221,204,154,118,134,146,382,67,194,303,84,176,96,81,49,32,25,47,98,298,126,69,95,220,108,52,38,43,57,168,113,76,50,77,118,126,50,48,69,128,118,60,40,34,29,96,74,156,97,134,76,77,34,27,23,26,55,162,193,124,54,69,51,23,23,35,37,311,315,224,35,58,51,22,28,72,277,25,78,118,168,65,58,35,27,14,14,382,92,116,220,101,69,53,33,39,32,110,558,94,221,96,77,48,32,26,60,110,270,303,357,176,204,168,128,89,75,34,83,118,221,64,176,113,109,42,27,38,52,108,220,95,69,126,298,98,47,35,54,69,168,96,76,59,65,194,110,51,66,74,118,101,60,44,30,47,138,32,60,75,84,65,77,52,21,25,33,24,41,47,71,52,69,56,34,35,16,23,47,91,118,115,52,45,35,16,15,170,64,65,113,154,65,61,32,32,20,378,240,79,126,204,101,64,46,27,54,67,194,303,84,176,96,81,49,32,25,85,126,298,221,316,176,220,168,88,69,27,42,109,113,176,64,221,118,83,34,29,55,83,118,204,95,66,116,270,74,41,49,78,76,154,96,77,33,78,110,23,46,49,54,115,101,79,29,23,52,15,22,53,35,86,65,81,52,26,12,12,26,52,81,65,86,35,53,22,15,52,23,29,79,101,115,54,49,46,23,110,78,33,77,96,154,76,78,49,41,74,270,116,66,95,204,118,83,55,29,34,83,118,221,64,176,113,109,42,27,69,88,168,220,176,316,221,298,126,85,25,32,49,81,96,176,84,303,194,67,54,27,46,64,101,204,126,79,240,378,20,32,32,61,65,154,113,65,64,170,15,16,35,45,52,115,118,91,47,23,16,35,34,56,69,52,71,47,41,24,33,25,21,52,77,65,84,75,60,32,138,47,30,44,60,101,118,74,66,51,110,194,65,59,76,96,168,69,54,35,47,98,298,126,69,95,220,108,52,38,27,42,109,113,176,64,221,118,83,34,75,89,128,168,204,176,357,303,270,110,60,26,32,48,77,96,221,94,558,110,32,39,33,53,69,101,220,116,92,382,14,14,27,35,58,65,168,118,78,26,276,72,28,22,51,58,36,224,315,310,37,35,23,23,51,69,54,125,193,161,55,26,23,27,34,77,76,134,98,155,74,96,29,34,40,60,118,128,69,48,50,126,118,77,50,76,113,168,57,43,38,52,108,220,95,69,126,298,98,47,25,32,49,81,96,176,84,303,194,68,382,145,134,118,154,204,222,749,558,379,374,62,25,35,51,77,113,304,327,867,584,63,43,28,51,69,118,298,241,239,924,229,20,15,36,51,45,47,754,1143,79,172,28,13,24,51,61,76,346,489,53,35,24,22,21,51,64,74,145,136,67,80,21,30,23,34,81,69,89,41,85,98,91,44,41,40,79,108,88,44,43,57,168,113,76,50,77,118,126,50,29,55,83,118,204,95,66,116,270,74,60,26,32,48,77,96,221,94,558,110,927,347,124,84,115,154,220,303,1044,866,1461,316,30,19,33,51,81,118,558,626,3567,2257,88,6,20,33,35,54,314,2022,371,1006,134,9,9,36,53,50,192,927,57,72,84,14,11,24,48,78,98,382,46,41,23,18,9,21,56,83,69,75,50,83,52,27,21,23,52,109,57,69,44,88,108,79,40,41,44,91,98,85,35,54,69,168,96,76,59,65,194,110,54,27,46,64,101,204,126,79,240,379,374,62,25,35,51,77,113,304,327,866,2022,754,224,71,86,115,168,298,558,1474};
int l,r,bl,br,ans=0;
template<class t>void rd(t &x){
x=0;int w=0;char ch=0;
while(!isdigit(ch)) w|=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x=w?-x:x;
}
bool magic(int x){
int ret=0,cnt=0,a[10];
while (x) a[cnt++]=x%10,x/=10;
if(!cnt) return 0;
if (cnt==1){
if (a[0]==7) return 1;
return 0;
}
for(int i=cnt-2;~i;--i) ret=ret*10+abs(a[i+1]-a[i]);
return magic(ret);
}
int main(){
rd(l),rd(r);
bl=(l-1)/base+1,br=(r-1)/base+1;
if (bl==br){
for(int i=l;i<=r;i++)
if(magic(i)) ++ans;
return printf("%d
",ans),0;
}
for(int i=l;i<=bl*base;i++) ans+=magic(i);
for(int i=bl+1;i<=br-1;i++) ans+=table[i];
for (int i=(br-1)*1e6+1;i<=r;i++) ans+=magic(i);
printf("%d
",ans);
return 0;
}