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 ...
随机推荐
- ES6-Set和Map数据结构学习笔记
Set和Map数据结构 Set 基本用法 ES6提供了新的数据结构--Set,类似于数组,但是成员的值是唯一的,没有重复的值,Set本身是一种构造函数,用来生成Set数据结构 var s = new ...
- 【WCF】利用WCF实现上传下载文件服务
引言 前段时间,用WCF做了一个小项目,其中涉及到文件的上传下载.出于复习巩固的目的,今天简单梳理了一下,整理出来,下面展示如何一步步实现一个上传下载的WCF服务. 服务端 1.首先新建一个名 ...
- jquery ajax中使用jsonp的限制(转)
http://www.cnblogs.com/dudu/archive/2012/12/04/jquery_ajax_jsonp.html jsonp 解决的是跨域 ajax 调用的问题.为什么要跨域 ...
- HDU4416Good Article Good sentence(后缀自动机)
Problem Description In middle school, teachers used to encourage us to pick up pretty sentences so t ...
- SQL夯实基础(五):索引的数据结构
数据量达到十万级别以上的时候,索引的设置就显得异常重要,而如何才能更好的建立索引,需要了解索引的结构等基础知识.本文我们就来讨论索引的结构. 二叉搜索树:binary search tree 1.所有 ...
- LeetCode Distribute Candies
原题链接在这里:https://leetcode.com/problems/distribute-candies/#/description 题目: Given an integer array wi ...
- 常用Request对象获取请求信息
Request.ServerVariables(“REMOTE_ADDR”) ‘获取访问IPRequest.ServerVariables(“LOCAL_ADDR”) ‘同上Request.Serve ...
- mybatis与oracle使用总结
Oracle使用总结 1.新建表删除表 新建表语句: CREATE TABLE +表名{ } create table AFA_USER ( USER_ID VARCHAR2() not null, ...
- Windbg内核调试之一: Vista Boot Config设置
Windbg进行内核调试,需要一些基本的技巧和设置,在这个系列文章中,我将使用Windbg过程中所遇到的一些问题和经验记录下来,算是对Kernel调试的一个总结,同时也是学习Windows系统内核的另 ...
- mongo之map-reduce笔记
package com.sy.demo; import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; import ...