题意有点难以描述,简略的就是给定一个二进制\(n\),每一步操作能使\(n\)的位为1的数的和转化为一个十进制,然后转化为该数的二进制再进行相同的操作

查询\([0,n]\)中操作数恰好为\(k\)的使该数最终降为1的个数

注意,\(1_{(2)}\)的操作数是0

n的范围非常大可以想到是数位DP,并且它递减得非常快所以k其实连10都不到就不需统计了

Update:发现代码部分地方写sb了,不过影响不大

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int MAXN = 5e5+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
const ll MOD = 1e9+7;
ll read() {
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int k;
char str[MAXN];
int a[MAXN],f[MAXN];
ll dp[1007][1007][11];
int F(int x){ // x(10)->1(2)
if(x<=1) return 0;
if(~f[x]) return f[x];
int cnt=0;
while(x){
cnt+=(x&1);
x>>=1;
}
return f[x]=1+F(cnt);
}
ll DP(int cur,int _1,int k,bool limit){
if(cur==0) return F(_1)==k-1;
if(!limit&&dp[cur][_1][k]!=-1) return dp[cur][_1][k];
int up=limit?a[cur]:1;
ll ans=0;
rep(i,0,up){
ans+=DP(cur-1,_1+i,k,limit&&a[cur]==i)%MOD;
ans%=MOD;
}
return limit?ans:dp[cur][_1][k]=ans;
}
ll solve(){
int len=strlen(str+1);
rep(i,1,len) a[i]=str[len-i+1]-'0';
return DP(len,0,k,1);
}
int main(){
memset(f,-1,sizeof f);
memset(dp,-1,sizeof dp);
while(~scanf("%s",str+1)){
k=read();
if(k>10){
println(0);
}else if(k==1){
ll tmp=solve();
tmp=((tmp-2)%MOD+MOD)%MOD;
println(tmp);
}else if(k==0){
println(1);
}
else{
println(solve());
}
}
return 0;
}

Codeforces - 914C 数位DP的更多相关文章

  1. Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)

    大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...

  2. Codeforces 55D (数位DP+离散化+数论)

    题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...

  3. Codeforces 628D 数位dp

    题意:d magic number(0<=d<9)的意思就是一个数,从最高位开始奇数位不是d,偶数位是d 题目问,给a,b,m,d(a<=b,m<2000)问,a,b之间有多少 ...

  4. codeforces 401D (数位DP)

    思路:很明显的数位dp,设dp[i][j] 表示选取数字的状态为i,模m等于j的数的个数,那么最后的答案就是dp[(1<<n)-1][0].状态转移方程就是,dp[i|(1<< ...

  5. codeforces 55D 数位dp

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  6. Shovel Sale CodeForces - 899D (数位dp)

    大意: n把铲子, 价格1,2,3,...n, 求有多少个二元组(x,y), 满足x+y末尾数字9的个数最多. 枚举最高位, 转化为从[1,n]中选出多少个二元组和为$x$, 枚举较小的数 若$n\g ...

  7. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  8. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  9. Codeforces Gym 100231L Intervals 数位DP

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...

随机推荐

  1. code1085 数字游戏

    划分dp 把环变链(读入4 3 -1 2变成4 3 -1 2 4 3 -1 2) 设dp[i][j][k]为把i~j分成k份,各部分内的数字相加,相加所得的k个结果对10取模后再相乘,最终得到的一个数 ...

  2. xml与java代码相互装换的工具类

    这是一个java操作xml文件的工具类,最大的亮点在于能够通过工具类直接生成xml同样层次结构的java代码,也就是说,只要你定义好了xml的模板,就能一键生成java代码.省下了自己再使用工具类写代 ...

  3. InteliJ中文乱码;IDE快捷键使用

    启动服务器的时候出现如图 解决方法: 对服务器的位置进行编辑 增加如图的信息 -Dfile.encoding=UTF-8

  4. jQuary总结3: jQuery语法1

    1.jQuery样式操作 1.1 设置或者修改样式,操作的是style属性. 单样式语法: jQuery对象.css('属性名', '属性值') //html <div id="box ...

  5. Swift实现Touch ID验证

    iOS8开放了很多API,包括HomeKit.HealthKit什么的.我们这里要说的是其中之一的Touch ID验证. 以前用app保护用户的隐私内容,只能设定和输入密码.眼看着只能是iPhone本 ...

  6. 关于super关键字与继承

    super它只是一个限定词,当用super引用时,它也是引用当前对象本身,只是super只是限定了访问当前对象从父类那里继承得到成员变量或方法. import java.util.Date; publ ...

  7. XE10开发的APP对于苹果IPV6上架要求的处理(DelphiTeacher)

    1.服务器必须使用域名.不能使用IP地址2.Indy的话,域名加[]3.DataSnap的话,Params.Values['CommunicationIPVersion'] :='IP_IPv6';4 ...

  8. Linq基础必备

    1.linq基础必备之对象初始化器和匿名类型因果分析   3. 一:对象初始化器 1.就是在new的时候给公共属性赋值的一种方式 2. 在没有初始化器之前的时候,我们是怎么初始化的呢??? 1. 构造 ...

  9. vcenter安装错误The DSN is pointing to anunspported ODBC driver...

    在安装vcenter server中采用现有独立sql server数据库时出现下列错误. 这是由于当前独立数据库版本和当前系统的客户端驱动不匹配.导致我们在odbc中配置dsn无法正常运行. 如sq ...

  10. 加密--HashPasswordForStoringInConfigFile过时问题

    最近公司在对一套代码进行重构,把原本的web form换成mvc. 刚刚好几天打算开始做下登录,登录则必然会涉及到密码加密的问题. 原本打算用旧的加密方法就行了,哪里知道其中的md5加密出现了这样的问 ...