Codeforces 914C Travelling Salesman and Special Numbers (数位DP)
题意:题目中定义了一种运算,把数字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)的更多相关文章
- Codeforces 914 C. Travelling Salesman and Special Numbers (数位DP)
题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...
- Codeforces 914C Travelling Salesman and Special Numbers:数位dp
题目链接:http://codeforces.com/problemset/problem/914/C 题意: 对数字x进行一次操作,可以将数字x变为x在二进制下1的个数. 显然,一个正整数在进行了若 ...
- Codeforces 374 C. Travelling Salesman and Special Numbers (dfs、记忆化搜索)
题目链接:Travelling Salesman and Special Numbers 题意: 给了一个n×m的图,图里面有'N','I','M','A'四种字符.问图中能构成NIMA这种序列最大个 ...
- Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)
大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为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 ...
- 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现. 进行一次操作过后. 得到的数字肯定是<=1000的 然后1000以下可以暴力做的. 则我们枚举第1步后得到的数字x是 ...
- 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 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- 20个面试题让你真正了解jQuery
1. jQuery 库中的 $() 是什么?(答案如下) $() 函数是 jQuery() 函数的别称, $() 函数用于将任何对象包裹成 jQuery 对象,接着你就被允许调用定义在 jQuery ...
- C# 6.0 (VS2015 CTP6)
/* C# 6.0 demo https://github.com/dotnet/roslyn/wiki/Languages-features-in-C%23-6-and-VB-14 */ using ...
- Red hat linux 下配置Java环境(jdk)
1.把jdk-6u25-linux-i586-rpm.bin 复制到redhat linux中,放到/usr/java 目录下,该目录是mkdir 的,并chmod 755 jdk-6u25-li ...
- Chrome Adobe flash player已过期怎么办
越来越多的朋友感受到了来自谷歌chrome新版浏览器的压力,因为有不少朋友在使用新版chrome浏览器看视频时,却出现了这样的提示:Adobe flash player已过期!怎么办啊? 有网友抱怨: ...
- Android EditText输入光标居于开头最开始位置
如果欲使EditText加载后的输入光标自动处于最开始处,可以通过设置EditText的android:gravity实现,设置android:gravity为left或者start即可,可以设置: ...
- BlockingQueue实现阻塞队列
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public cl ...
- hdoj-1285-确定比赛名次(拓扑排序)
题目链接 /* Name:hdoj-1285-确定比赛名次 Copyright: Author: Date: 2018/4/11 15:59:18 Description: 标准的拓扑排序模板题,注意 ...
- uva1583(暴力枚举或打表)
紫书上的代码是打表. 我的做法是暴力枚举.注意,有多个变量时,选择枚举哪一个变量会影响到时间效率,值得考虑.由于各位数字之和最大就是五个9的和为45,所以就枚举各位数字之和比较快. #include& ...
- 数字排列(n,m)(搜索与回溯)
题目描述: 设有n个整数的集合{1,2,…,n},从中取出任意r个数进行排列(r<n),试列出所有的排列. 代码如下: #include<iostream>#include<c ...
- 加密第四节_IPSec基本理论
加密第四节_IPSec基本理论 本节内容 IPSec简介 IPSec两种工作模式 判断隧道模式和传输模式 IPSec两种模型 IPSec两个数据库 IPSec基本理论 IPSec简介 提供了网络层的安 ...