[CodeChef-DGTCNT]Chef and Digits
题目大意:
若一个十进制数$x$(不含前导零)满足数码$i$恰好出现$t_i$次,则这个数是坏的,否则是好的。求区间$[L,R](1\le L,R\le10^{18})$中有多少好数。
思路:
显然可以将区间$[L,R]$拆成$[1,R],[1,L)$分别计算。考虑计算区间$[1,n]$中好数的个数,可以用类似数位DP的方法,对于长度与$n$相等的情况枚举与$n$的LCP和LCP前的数字,否则枚举长度及首位数字直接计算。当限制不存在时,显然可以通过组合数计算答案。加上限制可以用容斥来计算,极限数据时间复杂度约$O(2^{10}\cdot18\cdot10\cdot18)$。
#include<cstdio>
#include<cctype>
typedef long long int64;
inline int64 getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int64 x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int B=,M=;
int t[B],tmp[B],dig[M],spc[B];
int64 C[M][M],pow[B+][M];
inline int64 calc(int l) {
int64 ret=;
for(register int i=;i<=spc[]&&l>=;i++) {
if(tmp[spc[i]]<) return ;
ret*=C[l][tmp[spc[i]]];
l-=tmp[spc[i]];
}
ret*=pow[-spc[]][l];
return ret;
}
inline int64 count() {
int64 ret=;
for(register int i=;i<B;i++) tmp[i]=t[i];
for(register int i=;i<=dig[]-;i++) {
for(register int j=;j<B;j++) {
tmp[j]--;
ret+=calc(i-);
tmp[j]++;
}
}
for(register int i=dig[];i;i--) {
for(register int j=i==dig[];j<dig[i];j++) {
tmp[j]--;
ret+=calc(i-);
tmp[j]++;
}
tmp[dig[i]]--;
}
ret+=calc();
return ret;
}
inline int64 solve(int64 n) {
if(!n) return ;
for(dig[]=;n;n/=) dig[++dig[]]=n%;
int64 ret=;
for(register int i=;i<<<B;i++) {
for(register int j=spc[]=;j<B;j++) {
if((i>>j)&) spc[++spc[]]=j;
}
ret+=(__builtin_popcount(i)&?-:)*count();
}
return ret;
}
inline void init() {
for(register int i=;i<M;i++) {
for(register int j=C[i][]=;j<=i;j++) {
C[i][j]=C[i-][j-]+C[i-][j];
}
}
for(register int i=;i<=B;i++) {
for(register int j=pow[i][]=;j<M;j++) {
pow[i][j]=pow[i][j-]*i;
}
}
}
int main() {
init();
for(register int T=getint();T;T--) {
const int64 l=getint(),r=getint();
for(register int i=;i<B;i++) t[i]=getint();
printf("%lld\n",solve(r)-solve(l-));
}
return ;
}
[CodeChef-DGTCNT]Chef and Digits的更多相关文章
- [Codechef CHSTR] Chef and String - 后缀数组
[Codechef CHSTR] Chef and String Description 每次询问 \(S\) 的子串中,选出 \(k\) 个相同子串的方案有多少种. Solution 本题要求不是很 ...
- 【Codechef】Chef and Bike(二维多项式插值)
something wrong with my new blog! I can't type matrixs so I come back. qwq 题目:https://www.codechef.c ...
- 【CodeChef】Chef and Graph Queries
Portal --> CC Chef and Graph Queries Solution 快乐数据结构题(然而好像有十分优秀的莫队+可撤销并查集搞法qwq) 首先考虑一种方式来方便一点地..计 ...
- [CodeChef - GERALD07 ] Chef and Graph Queries
Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...
- CodeChef CHEFSOC2 Chef and Big Soccer 水dp
Chef and Big Soccer Problem code: CHEFSOC2 Tweet ALL SUBMISSIONS All submissions for this prob ...
- Codechef FNCS Chef and Churu
Disciption Chef has recently learnt Function and Addition. He is too exited to teach this to his fri ...
- CodeChef - FNCS Chef and Churu(分块)
https://vjudge.net/problem/CodeChef-FNCS 题意: 思路: 用分块的方法,对每个函数进行分块,计算出该分块里每个数的个数,这样的话也就能很方便的计算出这个分块里所 ...
- 【xsy2111】 【CODECHEF】Chef and Churus 分块+树状数组
题目大意:给你一个长度为$n$的数列$a_i$,定义$f_i=\sum_{j=l_i}^{r_i} num_j$. 有$m$个操作: 操作1:询问一个区间$l,r$请你求出$\sum_{i=l}^{r ...
- codechef T2 Chef and Sign Sequences
CHEFSIGN: 大厨与符号序列题目描述 大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含‘<’.‘=’和‘>’三种比较符号的字符串. 记字符串长度为 N,大厨想要在字符串的开头.结尾 ...
- CodeChef - UASEQ Chef and sequence
Read problems statements in Mandarin Chinese and Russian. You are given an array that consists of n ...
随机推荐
- codeforces 110E Lucky Tree
传送门:https://codeforces.com/contest/110/problem/E 题意:给你一颗树,节点与节点之间的边有一个边权,定义只由4和7组成的数字是幸运数字,现在要你求一共有多 ...
- spring 中的@Import注解和@ImportResource注解
概述:@Import注解是引入带有@Configuration的java类. @ImportResource是引入spring配置文件.xml 案例的核心代码如下: package com.timo. ...
- 几种list集合的区别
SDK提供了有序集合接口java.util.List的几种实现,其中三种最为人们熟知的是Vector.ArrayList和LinkedList.有关这些List类的性能差别是一个经常被问及的问题.在这 ...
- shell监控进程是否存在
1.直接给代码:我这个是两个程序.多半要写成函数 [root@java1 src]# cat checkprocess.sh #!/bin/bashcheckprocess(){ps -ef|grep ...
- 【BZOJ1976】能量魔方 [最小割]
能量魔方 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 小C 有一个能量魔方,这个魔方可神奇 ...
- 51nodeE 斜率最大
题目传送门 这道题只要证明最佳解一定在相邻两个点之间的好啦 这个自己证一证就okay啦 而且我发现n方的算法可以过耶... #include<cstdio> #include<cst ...
- 【洛谷 P1653】 猴子 (并查集)
题目链接 没删除调试输出,原地炸裂,\(80\)->\(0\).如果你要问剩下的\(20\)呢?答:数组开小了. 这题正向删边判连通性是很不好做的,因为我们并不会并查集的逆操作.于是可以考虑把断 ...
- python通过POST提交页面请求
http://blog.csdn.net/liyzh_inspur/article/details/6294292 #网页POST提交数据 import urllibimport urllib2url ...
- linux下ntp服务器搭建方法
环境 软件:fedora14,装在virtualbox虚拟机上 硬件:x86 具体步骤 检查是否安装了ntp 运行如下命令: rpm -qa | grep ntp 如果有如下输出,表示有安装ntp 服 ...
- python 多进程锁Lock和共享内存
多进程锁 lock = multiprocessing.Lock() 创建一个锁 lock.acquire() 获取锁 lock.release() 释放锁 with lock: 自动获取.释放锁 类 ...