【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
会发现。
进行一次操作过后。
得到的数字肯定是可以写一个数位dp的。
即统计二进制数字y,满足y中1的个数为x然后y<=n
(一旦已经小于n了,则直接用组合数算方案就可以了
k=0和k=1要特殊处理一下
具体看代码
【代码】
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e3;
const ll MOD = 1e9+7;
int k,a[N+10],n;
ll ans = 0,C[N+10][N+10];
string s;
vector<int> v;
int GetNext(int x){
int y = 0;
while (x){
y+=(x&1);
x>>=1;
}
return y;
}
void dfs(int pos,int rest,bool xiao){
if (n-pos+1<rest) return;
if (rest==0){
ans = (ans+1)%MOD;
return;
}
if (pos>n) return;
if (xiao){
ans = (ans + C[n-pos+1][rest])%MOD;
return;
}
if (a[pos]==1){
dfs(pos+1,rest-1,xiao);
dfs(pos+1,rest,true);
}else{
if (xiao) dfs(pos+1,rest-1,xiao);
dfs(pos+1,rest,xiao);
}
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
for (int i = 0;i <=N;i++)
C[i][i] = C[i][0] = 1;
for (int i = 1;i <= N;i++)
for (int j = 1;j <= i-1;j++)
C[i][j] = (C[i-1][j]+C[i-1][j-1])%MOD;
cin >> s;
cin >>k;
for (int i = 0;i<(int) s.size();i++)
a[i+1] = s[i]-'0';
n = (int)s.size();
if (k==0){
cout <<1<<endl;
return 0;
}
//what if n==1 k==0
for (int i = 1;i <=N;i++){
int x = i;
int step = 0;
while (x!=1){
step++;
x=GetNext(x);
}
if (step==(k-1)) {
v.push_back(i);
}
}
for (int i = 0;i<(int)v.size();i++){
dfs(1,v[i],false);
}
if (k==1) {
ans = (ans-1+MOD)%MOD;
}
cout << ans << endl;
return 0;
}
【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers的更多相关文章
- Codeforces 914C Travelling Salesman and Special Numbers:数位dp
题目链接:http://codeforces.com/problemset/problem/914/C 题意: 对数字x进行一次操作,可以将数字x变为x在二进制下1的个数. 显然,一个正整数在进行了若 ...
- 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 (数位DP)
题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...
- Codeforces 374 C. Travelling Salesman and Special Numbers (dfs、记忆化搜索)
题目链接:Travelling Salesman and Special Numbers 题意: 给了一个n×m的图,图里面有'N','I','M','A'四种字符.问图中能构成NIMA这种序列最大个 ...
- 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 ...
- Codeforces 914C Travelling Salesman and Special Numbers (数位DP)
题意:题目中定义了一种运算,把数字x变成数字x的二进制位数.问小于n的恰好k次运算可以变成1的数的个数(题目中的n是二进制数,n最大到2^1000) 思路:容易发现,无论多么大的数,只要进行了一次运算 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块
Educational Codeforces Round 71 (Rated for Div. 2)-F. Remainder Problem-技巧分块 [Problem Description] ...
- Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题
Educational Codeforces Round 71 (Rated for Div. 2)-E. XOR Guessing-交互题 [Problem Description] 总共两次询 ...
随机推荐
- php实现自动加载类
PHP 实现自动加载类:
- Hexo Next 调优
Links对齐 编辑themes/next/source/css/_custom下的custom.styl,添加如下代码即可. .links-of-blogroll-title{ margin-lef ...
- springboot实现热部署,修改代码不用重启服务
1.引入热部署依赖 <!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupI ...
- 05001_Linux简介
1.Linux的概述 (1)Linux是基于Unix的开源免费的操作系统,由于系统的稳定性和安全性几乎成为程序代码运行的最佳系统环境.Linux是由Linus Torvalds(林纳斯•托瓦兹)起初开 ...
- django-xadmin定制之分页显示数量
环境:xadmin-for-python3 python3.5.2 django1.9.12 主要思路:利用django-xadmin的插件原理和原有分页插件的逻辑,单独定义一个分页显示数插件,效果如 ...
- mysql生成随机时间
mysql> update test set create_time=concat('2013-10-01 ', floor(10+rand()*10),':',floor(10+rand()* ...
- android 选取部分 log 的两种方法
Grep多个条件: android logcat -v time | grep -e A -e B 选取多个android log tag: android logcat -v time -s TAG ...
- [Java开发之路](6)File类的使用
1. 构造方法 构造方法 描写叙述 File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建一个新的文件实例. File(String parent , String ...
- flowable一个简单的例子
holiday-request.bpmn20.xml: <?xml version="1.0" encoding="UTF-8"?> <def ...
- 15:Challenge 11(主席树裸题)
总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...