题意有点难以描述,简略的就是给定一个二进制\(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. poj 3304 Segments (题意理解出错,错误的只枚举了过线段的直线)

    //枚举过每一条线段的直线, //再判断其他线段的点在直线上或被直线穿过 //即求直线与线段相交(叉积) #include<stdio.h> #include<math.h> ...

  2. C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿![转载]

    说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...

  3. windows 安装 mysql5.7.17

    下载mysql 进入官网:https://www.mysql.com/ 单击[Downloads]选项卡 最下面有个[  MySQL Community Edition (GPL)],单击[Commu ...

  4. 【转载】实战mysql分区(PARTITION)

    转载地址:http://lobert.iteye.com/blog/1955841 前些天拿到一个表,将近有4000w数据,没有任何索引,主键.(建这表的绝对是个人才) 这是一个日志表,记录了游戏中物 ...

  5. HDU 2159 FATE (二维背包)

    题意:中文题. 析:dp[i][j] 已经杀了 i 个怪兽,已经用了 j 体积,所能获得的最大经验值,这个和一维的差不多,只是加一维而已. 代码如下: #pragma comment(linker, ...

  6. 用Word 写csdn blog

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  7. SuSE Linux上修改主机名

    1) 临时修改主机名 临时修改使用hostname即可,格式为:hostname 新主机名.Hostname命令除可以临时修改主机名外,还可以用它来查看主机名,不带参数执行它,即为查看主机名. 2)  ...

  8. 获取host信息

    QT如果要进行网络编程首先需要在.pro”中添加如下代码: QT += network 在头文件中包含相关头文件: #include <QHostInfo> #include <QN ...

  9. mysql查询最近7天的数据,没有数据自动补0

    问题描述 查询数据库表中最近7天的记录 select count(*),date(create_time) as date from task where datediff(now(),create_ ...

  10. 微信openid

    微信openid由用户id和公众号id加密而来,同一用户相对同一公众账号的openid是不变的.