[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 ...
随机推荐
- POJ3159:Candies(差分约束)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 39666 Accepted: 11168 题目链接:h ...
- 如何把SSL公钥和私钥转化为PFX格式
1.登陆 https://myssl.com/cert_convert.html 2.原格式选择为 “PEM”,目标格式选择为 “PKCS12” 3.上传cer到 ”证书文件“,上传key到 ”私 ...
- Java输入输出流备忘
重要博客: http://blog.csdn.net/hguisu/article/details/7418161 File dir = new File("\\root"); ...
- [洛谷P3501] [POI2010]ANT-Antisymmetry
洛谷题目链接:[POI2010]ANT-Antisymmetry 题目描述 Byteasar studies certain strings of zeroes and ones. Let be su ...
- bzoj1251: 序列终结者 fhqtreap写法
fhqtreap的速度果然很快 花了时间学了下指针写法 没有旋转 只有分裂以及合并操作 其实还是蛮好写的 #include<cstdio> #include<cstring> ...
- codeforces739C - Skills &&金中市队儿童节常数赛
http://codeforces.com/problemset/problem/739/C 先上链接 这道题 对于蒟蒻的我来说还是很有难度的 调了很久 对于我的代码 mx2是答案 mx1代表单调 m ...
- 2017年上海金马五校程序设计竞赛:Problem C : Count the Number (模拟)
Description Given n numbers, your task is to insert '+' or '-' in front of each number to construct ...
- Laravel - Property [title] does not exist on this collection instance
When you're using get() you get a collection. In this case you need to iterate over it to get proper ...
- 在生成的Debug中test.exe的同级目录下创建一个文件,如TestLog.log
在上次编写一个日志类库时,想在.exe的同级目录下创建.log文件,对于这个路径的获得很简单,调用GetModuleFileName()函数即可.但是要去掉.exe而换成.log时,由于对字符串处理不 ...
- Linux-进程间通信(一): 管道
1. 管道局限性: (1) 半双工:(若模拟全双工,可以使用两个管道,即,proc1-->proc2一条管道,proc2-->proc1一条管道) (2) 只能在具有公共祖先的进程之间使用 ...