【2019.9.25】Za

容斥

容斥 \(\Big|\bigcup\limits_{i=1}^nS_i\Big|=\sum\limits_{m=1}^n(-1)^{m-1}\ \sum\limits_{a_i<a_{i+1}}\Big|\bigcap\limits_{i=1}^mS_{a_i}\Big|\)

不定方程非负整数解计数

不定方程\(\sum_{i=1}^n\ x_i=m\)和\(n\)个限制条件\(x_i\le 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}$个
  1. 元素:变量\(x_i\)
  2. 属性:\(x_i\)的属性即其限制条件

目标:满足对应关系时集合大小\(\Big|\bigcap_{i=1}^n\ S_i\Big|\)

\(\Big|\bigcap_{i=1}^n\ S_i\Big|=\Big|\bigcup\Big|-\Big|\bigcup_{i=1}^n\overline{S_i}\) 即变为对于一些

HAOI2008硬币购物

即可转化为求方程\(\sum_{i-1}^4\ C_ix_i=S,x_i\le D_i\)的非负整数解的个数

最后求解\(\sum\limits_{i=1}^4C_ix_i=S-\sum\limits_{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\n",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\n",ans);
    }
    return 0;
}

\(n\)的错位排列数为

\(D_n=n!-n!\sum\limits_{k=1}^n\frac{(-1)^{k-1}}{k!}=n!\sum\limits_{k=0}^n\frac{(-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\n",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\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/lxyyyy/p/11587949.html

时间: 2024-10-29 20:20:14

【2019.9.25】Za的相关文章

【2019.10.25】

summary 分情况拿分保底真的很好用 像我这种辣鸡应该注意保底 打题不要慌 有条理 不要东一条西一条 小奇采药 对于 30% 的数据,O(2n ) 枚举取 or 不取 对于 60% 的数据,O(nm) 做 01 背包,即 f(i, j) 表示前 i 株 草药,耗费 j 的时间能达到的最?代价. 对于 100% 的数据,注意到 m,t,v 纯随机 那么不会选太多的草药,?耗时较少的草药有很?概率存在 于最优解中 针对这些性质优化搜索 当然也可以合理使用随机化和卡时,复杂度 O(?学) 最基础6

【2019.11.1】ZA

noip2012 Vigenère 密码 模拟 用的以前的方法 然后我康了康yyb的 发现我好菜 int r[30][30]; void pre(){ for(int i=1;i<=26;++i) for(int j=0;j<=26-i;++j) r[i][i+j]=j+1; for(int i=2;i<=26;++i) for(int j=28-i;j<=26;++j) r[i][j-27+i]=j; } int main(){ #ifndef ONLINE_JUDGE freo

【2019最新版】免接口!!涛舅舅轻奢版视频强制裂变程序,免公众号免备案域名!

轻奢版视频强制裂变程序[免接口版]正式上线,不需要认证公众号和备案域名! 优化防封和跳转逻辑,入口.落地.分享域名全部隔离 并加入针对微信IOS版的特殊防封策略! 除了分享卡片没有“小字描述”和“分享图标”以外,其他功能和[接口版]基本无差异! 轻奢版视频免接口版主要更新如下: 1.代码完全重构,大范围增强了程序的健壮性! 2.加入FuckXX脚本,禁止一切XX节点的访问,优化防封! 3.加入来路判断,非法访问将禁止,强化防封! 4.数据全部缓存化,前台完全避免对数据库的请求调用,所有数据调用全

【2019.10.25 OI-Killer的模拟赛】3.鸡数

题目链接 题意: 定义“鸡数”指从高位到低位单调不减的数.求$[a,b]$之间有多少个“鸡数”.$t$组询问. $1\le t\le 10^5,\; 1\le a\le b\le 2^{31}-1$ 分析: 数位DP.设$f[i][j]$表示长度为$i$,最高位是$j$的“鸡数”个数,那么$$f[i][j]=\sum\limits^9_{k=j}f[i-1][k]$$ 且$$f[1][i]=1\;(1\le i\le 9)$$ 那么对于一个长度为$l$的$n$且由低到高位写成$s_{1\dots

【2019南京赛】

hello 2019:题意: 给出区间,要你在查询区间内有9102,同时没有8012的序列,需要删除多少个数字. 多次询问,所以给上我们的线段树. 这道题有原题: 我们dp表示i从状态i到状态j的操作次数, 我们定义0,1,2,3,4为"",2,20,201,2017的状态.对于每个状态用矩阵表示: 如dp[1][2]从状态为2到20: 例如我们看其中一个地方{ if(s[l]=='2') p[cur].a[0][0]=1,p[cur].a[0][1]=0; 表示此刻遇到了2,那么就0

【2019最新资料】米尔iMX8M系列开发板MYD-JX8MX功能清单

2019年6月,米尔推出的基于NXP iMX8M系列芯片的开发平台MYD-JX8MX系列开发板,已开始量产.下面介绍这款开发板的功能:MYD-JX8MX 集成了丰富的外设资源,充分发挥了 i.MX8M 的产品性能,并定向目前主流产品应用提供了专用外设,主要特色资源如下:1.丰富的音视频接口(1) 双路视频输出:a.1 路为 HDMI 输出,支持 4K 分辨率高清输出b. 另一路由主芯片 MIPI-DSI 接口直接引出,同时在电路上将该 MIPI-DSI 转接为 LVDS 电路,客户根据屏幕情况二

【Python】【demo实验25】【练习实例】

原题: 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. 我的源码: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- # 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和. m = [1,2] n = [2,3] s = 0 for i in range(20): if (len(m) < i + 1): m.append(m

【2019.11.5】

2019.11.5 开方 可以找到规律der 然后特判 开方五次最大为\(2^{32}-1\) 注意可能有前导零 要注意特殊数据\(0,1\)== 然后我没有注意到\(1\)... 行叭我是瘟猪 int main(){ //freopen("sqrt.in","r",stdin); //freopen("sqrt.out","w",stdout); for(;scanf("%s",s+1)!=EOF;){

【英语学习】【17/3/25】

1.Such expressions rather grate on me. 这种说法对我来说很刺耳. 2.smell有n.气味.vt.& vi.嗅的意思.而odour只有n.气味的含义,不能用作动词. 3.vi与vt. vi(intransitive verb)表示不及物动词,intransitive是不及物的意思,而transitive是及物的意思,因此vt表示及物动词. 4.