cf 834 E. Ever-Hungry Krakozyabra(爆搜+数位dp)

题意:

定义一种inedible tail为一个数把每一位数字按不降的顺序排列后,去掉前导0组成的序列

比如57040 组成的就是457 54组成就是45 45组成的也是45

问区间\([L,R]\)内有多少种inedible tail

题解: 直接数位dp做,需要存状态,太大处理不了。

这个问题等价于\(x0+x1+x2+...x9=18\)的非负整数解

组合数学 插空法 \(18+9个1,选9个插空,其余变1就是解,C(18+9,9) \approx 4*10^{6}\)

所以可以暴力枚举出所有方案,然后快速判断这种方案在\([L,R]\)内是否合法

用类似数位dp的思想,用上下界来枚举每一位能取的数字,到到达某一位时即不在上界也不在下界,说明后面的数字可以随便取,那么一定取得出这种方案

由于只有在上界或者下界的时候递归才会继续往下走,所以最多走18次,线性复杂度判断

#include<bits/stdc++.h>
#define LL long long
#define P pair<int,int>
#define ls(i) seg[i].lc
#define rs(i) seg[i].rc
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls rt<<1
#define rs (rt<<1|1)
using namespace std;
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
return x;
}
int pos,ans;
int a[20],b[20];
int cnt[10];///数字为i的个数
bool is(int pos,bool Lbound,bool Rbound){
if(pos == 0) return true;
if(!Lbound && !Rbound) return true;///不是上下界,后面的数字可以任意取一定存在合法的情况
int l = Lbound?a[pos]:0;
int r = Rbound?b[pos]:9;
if(l > r) return false;
for(int i = l;i <= r;i++){///枚举每一位能取的数字
if(cnt[i]){
cnt[i]--;
if(is(pos - 1,Lbound && i == l, Rbound && i == r)){
cnt[i]++;
return true;
}
cnt[i]++;
}
}
return false;
}
void dfs(int cur,int total){
if(cur == 9){
cnt[cur] = total;
ans += is(pos,1,1);
return ;
}
for(int i = 0;i <= total;i++){
cnt[cur] = i;
dfs(cur + 1, total - i);
}
}
int digit(int *d,LL x){
int pos = 0;
while(x){
d[++pos] = x % 10;
x /= 10;
}
return pos;
}
int main(){ LL L,R;
cin>>L>>R;
pos = digit(a,L);
pos = digit(b,R);
ans = 0;
dfs(0,pos);///暴力枚举所有的组合
cout<<ans<<endl;
return 0;
}

cf 834 E. Ever-Hungry Krakozyabra的更多相关文章

  1. 高斯分布与Gamma分布关系

    https://math.stackexchange.com/questions/1917647/proving-ex4-3%CF%834

  2. CF 327B. Hungry Sequence

    B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. cf B. Hungry Sequence

    http://codeforces.com/contest/327/problem/B 这道题素数打表就行. #include <cstdio> #include <cstring& ...

  4. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  7. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  8. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  9. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

随机推荐

  1. 基于Xtrabackup恢复单个innodb表

      Preface       We all know that Xtrabackup is a backup tool of percona for innodb or Xtradb.It's us ...

  2. LVS基于DR模式搭建负载均衡群集

    LVS -DR模式集群架构原理图

  3. 用python写一个类似于linux中的tree

    import os filePath = 'g:/File' j = 0 # 查找的深度计数 def tree(filePath,j): dir_now = os.listdir(filePath) ...

  4. ubuntu配置命令

    sudo apt-get update 更新源 sudo apt-get install package 安装包 sudo apt-get remove package 删除包 sudo apt-ca ...

  5. 如果文件里是汉字的话,这地方seek括号里面只能是偶数

    >>> f=open("E:/pythonLearn/140.txt") >>> f.seek(8) #如果文件里是汉字的话,这地方seek括号 ...

  6. POJ 1854 贪心(分治)

    Evil Straw Warts Live Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1144   Accepted:  ...

  7. python——input()函数

    在使用input()内置函数输入数字时,要注意一点: input()的返回值始终是字符串,所以type(number)永远是<class 'str'>! 如: >>> t ...

  8. POJ :3614-Sunscreen

    传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...

  9. [Link-Cut-Tree][BZOJ2002]弹飞绵羊

    题面 Description 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上\(n\)个 ...

  10. <jsp:param>传参乱码问题

    在添加参数的界面添加<%request.setCharacterEncoding("UTF-8");%> 实例代码: login_confirm.jsp <%@ ...