题意:题目中定义了一种运算,把数字x变成数字x的二进制位数。问小于n的恰好k次运算可以变成1的数的个数(题目中的n是二进制数,n最大到2^1000)

思路:容易发现,无论多么大的数,只要进行了一次运算,一定会变成1000以内的数,所以我们可以预处理1000以内的数经过多少次运算到1。之后,我们可以枚举1000以内的数字,枚举有哪些数字是经过k - 1次运算到1(假设此时数字是i),那么小于n的位数为i的数字都是答案。怎么统计答案呢?我们用试填法。我们dfs中传入3个参数:deep(当前搜索到第几位), flag(判断后面的位可不可以随便填),remain(还剩多少位没有填)。我们判断第deep位可以填什么。如果n的第deep位是1,那么填1填0都可以,分别搜索。如果第deep位是0,那么只能填0。

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL mod = 1000000007;
const int maxn = 1010;
LL C[maxn][maxn];
LL num[maxn];
LL f[maxn][maxn];//i位,需要j步到1的
char s[maxn];
LL ans;
int n;
void dfs(int deep, bool flag, int remain) {
if(n < remain) return;
if(remain == 0) {
ans = (ans + 1) % mod;
return;
}
if(deep < 1) return;
if(flag == 0) {
ans = (ans + C[deep][remain]) % mod;
return;
}
if(s[deep] == '1') {
dfs(deep - 1, 0, remain);
dfs(deep - 1, flag, remain - 1);
} else {
dfs(deep - 1, flag, remain);
}
}
int main() {
int m;
scanf("%s",s + 1);
scanf("%d", &m);
n = strlen(s + 1);
if(m == 0) {
printf("1\n");
return 0;
} else if(m == 1) {
printf("%d\n", n - 1);
return 0;
}
for (int i = 0; i <= n; i++) C[i][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++) {
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
reverse(s + 1, s + 1 + n);
for (int i = 2; i <= 1000; i++) {
int cnt = 0;
for (int j = i; j; j >>= 1) {
cnt += (j & 1);
}
num[i] = num[cnt] + 1;
if(num[i] + 1 == m)
dfs(n, 1, i);
}
printf("%lld\n", ans);
}

  

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

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

    题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...

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

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

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

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

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

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

  5. 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 ...

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

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

  7. 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 ...

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

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

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

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

随机推荐

  1. hdu6242 计算几何

    题意:给你n个点,要求找到一个点,和一个圆心,使得有n/2向上取整个点在圆上,一定有满足条件的点存在 题解:既然一定有解,而且圆上有n/2向上取整个点,那么我们可以通过随机来找三个点来确定一个圆心,和 ...

  2. ural 2014 Zhenya moves from parents

    2014. Zhenya moves from parents Time limit: 1.0 secondMemory limit: 64 MB Zhenya moved from his pare ...

  3. mac下编译安装grafana 4.2.0

    go语言在开发效率和运行效率中的优势让很多人青睐,所以有倾向打算转向go语言的开发. 下面介绍在Mac OS X中golang的开发环境配置. 1.安装brew brew是一个mac下的由ruby开发 ...

  4. (转)轻量级C语言实现的minixml解析库入门教程

    svn上的minixml源码下载:svn co http://svn.msweet.org/mxml/tags/release-2.7/ 按照下载回来的源代码进行编译和安装.本教程只针对新手做一个引导 ...

  5. Linux C 编程内存泄露检测工具(一):mtrace

    前言 所有使用动态内存分配(dynamic memory allocation)的程序都有机会遇上内存泄露(memory leakage)问题,在Linux里有三种常用工具来检测内存泄露的情況,包括: ...

  6. OpenCV那个版本的比较好用、稳定,参考资料也较多的

    2.4.8.上opencv官网就能下载到,对应不同版本的VS有编译好的文件. 2.4以后的变化不大.所以你可以百度opencv,有中文网站,上面有详细的说明.如果是在windows系统的话,可以使用v ...

  7. 性能测试工具BenchmarkDotnet

    .NET Core中的性能测试工具BenchmarkDotnet https://www.cnblogs.com/lwqlun/p/9671611.html 背景介绍 之前一篇博客中,我们讲解.NET ...

  8. GMchess Linux下的中国象棋游戏

    gmchess,一款Linux下的中国象棋程序

  9. BZOJ4009:[HNOI2015]接水果(整体二分版)

    浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html 题目传送门:https://lydsy.com/JudgeOnline/problem.p ...

  10. VS2013 中使用 CxImage 库时用Unicode编码时出现链接错误

    CxImage 本身是支持Unicode 编码的,编译CxImage库的时候选择编译Unicode就可以了,得到的lib文件和dll文件很容易看出有个u的就是Unicode编码的 当然在使用的时候要对 ...