题目链接:Travelling Salesman and Special Numbers

题意:

  给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7->3),现在给出了一个数k,问不大于n的数中有几个数经过k次操作可以变成1。

题解:

  因为所给的n很大,但是可以发现只要经过一次操作,n都会变成1000以内的数,所以可以把1000以内的数的答案都存下来。每次在这里面找等于k-1的数,然后数位DP求个数。

 #include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e3+;
const int MOD = 1e9+;
char s[MAX_N];
int len;
int f[MAX_N][MAX_N];
long long res[MAX_N];
void init()
{
memset(f,-,sizeof(f));
for(int i=;i<=;i++)
{
int t = i;
int num = ;
while(t>)
{
int temp = ;
while(t)
{
t -= t&-t;
temp++;
}
t = temp;
num++;
}
res[i] = num;
}
}
int dp(char *s,int pos,int num,int tar,int limit)
{
if(num > tar || tar>) return ;
if(pos == len)
{
if(num == tar) return ;
else return ;
}
if(!limit && f[pos][tar-num]!=-) return f[pos][tar-num];
int up = limit?s[pos]-'':;
int ans = ;
for(int i=;i<=up;i++) ans = (ans + dp(s,pos+,i==?num+:num,tar,limit&&i==up))%MOD;
if(!limit) f[pos][tar-num] = ans;
return ans;
}
int main()
{
init();
scanf("%s",s);
len = strlen(s);
int num = ;
int k;
cin>>k;
if(k==)
{
cout<<<<endl;
return ;
}
if(k==)
{
cout<<len-<<endl;
return ;
}
long long ans =;
for(int i=;i<len;i++) if(s[i] == '') num++;
for(int i=;i<=;i++)
{
if(res[i] == k-)
{
ans = (ans + dp(s,,,i,))%MOD;
//cout<<"...."<<i<<"....."<<dp(s,0,0,i,1)<<endl;
}
}
cout<<ans<<endl;
}

Codeforces 914 C. Travelling Salesman and Special Numbers (数位DP)的更多相关文章

  1. Codeforces 914 C Travelling Salesman and Special Numbers

    Discription The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pas ...

  2. Codeforces 914C Travelling Salesman and Special Numbers (数位DP)

    题意:题目中定义了一种运算,把数字x变成数字x的二进制位数.问小于n的恰好k次运算可以变成1的数的个数(题目中的n是二进制数,n最大到2^1000) 思路:容易发现,无论多么大的数,只要进行了一次运算 ...

  3. Codeforces 374 C. Travelling Salesman and Special Numbers (dfs、记忆化搜索)

    题目链接:Travelling Salesman and Special Numbers 题意: 给了一个n×m的图,图里面有'N','I','M','A'四种字符.问图中能构成NIMA这种序列最大个 ...

  4. Codeforces 914C Travelling Salesman and Special Numbers:数位dp

    题目链接:http://codeforces.com/problemset/problem/914/C 题意: 对数字x进行一次操作,可以将数字x变为x在二进制下1的个数. 显然,一个正整数在进行了若 ...

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

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

  6. 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现. 进行一次操作过后. 得到的数字肯定是<=1000的 然后1000以下可以暴力做的. 则我们枚举第1步后得到的数字x是 ...

  7. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

  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 Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

随机推荐

  1. .Oracle固定执行计划之SQL PROFILE概要文件

    1.  引子Oracle系统为了合理分配和使用系统的资源提出了概要文件的概念.所谓概要文件,就是一份描述如何使用系统的资源(主要是CPU资源)的配置文件.将概要文件赋予某个数据库用户,在用户连接并访问 ...

  2. [UI] Article intro effects

    Article intro effects http://freebiesbug.com/code-stuff/article-intro-effects/

  3. [翻译] DoImagePickerController

    DoImagePickerController An image picker controller with single selection and multiple selection. Sup ...

  4. ELK搭建实时日志分析平台之一ElasticSearch搭建

    文:铁乐与猫 系统:CentOS Linux release 7.3.1611 (Core) 注:我这里为测试和实验方便,ELK整套都装在同一台服务器环境中了,生产环境的话,可以分开搭建在不同的服务器 ...

  5. vs环境变量学习

    1. 查看vs环境变量: 在项目设置中的任何路径.目录编辑项目下,右下角有个“宏”,点开即可见所有vs环境变量的当前设置...听说还有其它地方,没看到. 2.上边的“宏”,即是英文的vs环境变量 3. ...

  6. MySQL知识总结(四)二进制日志

    1 定义 bin-log日志记录了所有的DDL和DML的语句,但不包括查询的语句,语句以事件的方式保存,描述了数据的更改过程,此日志对发生灾难时数据恢复起到了极为重要的作用. 2 开启 mysql默认 ...

  7. 10条Linux 命令了解服务器当前性能

    参考:http://www.infoq.com/cn/news/2015/12/linux-performance 1. uptime 如果电脑运行缓慢,执行 uptime 可以大致查看Linux服务 ...

  8. VMware常见错误故障排查

    1. VMware安装失败 “Failed to create the requested registry key Key:installer Error:1021" 1.1. windo ...

  9. Mysql数据库的mysql Schema 究竟有哪些东西& 手工注入的基础要领

    #查看数据库版本号 mysql> select @@version; +------------+ | @@version  | +------------+ | 5.5.16-log | +- ...

  10. BZOJ1597:[USACO]土地购买(斜率优化DP)

    Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 < = 1,000 ...