【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] 总共两次询 ...
随机推荐
- [POI2011]MET-Meteors 整体二分_树状数组_卡常
线段树肯定会 TLE 的,必须要用树状数组. Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorith ...
- HDU-1878 欧拉回路 欧拉回路
题目链接:https://cn.vjudge.net/problem/HDU-1878 题意 中文题,而且就是单纯的欧拉回路 思路 判断连通图 用并查集会很好,bfs亦可 一时脑抽用bfs过了这个题, ...
- sed命令的介绍
命令格式 sed [options] 'command' file(s) sed [options] -f scriptfile file(s) 选项 -e<script>或--expre ...
- unity C# 获取有关文件、文件夹和驱动器的信息
class FileSysInfo { static void Main() { // You can also use System.Environment.GetLogicalDrives to ...
- 二 MapReduce 各阶段流程分析
如果想要将问题变得清晰.精准和优雅, 需要关注 MapReduce 作业所需要的系统资源,尤其是集群内部网络资源使用情况. MR 可以运行在共享集群上处理 TB 级 甚至 PB 级的数据.同时,改作业 ...
- windows開始菜单和任务栏图标显示空白而且点击时候显示项目已被移动或删除
这几天实验室老常常自己主动断电.这是非常蛋疼的一件事,这不上次断电就出事了.来电后开机,点击任务栏上的程序全都显示为无法打开此项目,该项目已被移动.删除.原因是图标缓存丢失,可能是突然断电引起的,也有 ...
- 菜鸟之webservice(一) 服务端搭建
首先说一下,为什么取名叫菜鸟之webservice,由于本人技术真的不咋滴,写博客仅仅是为了对所学知识的总结.webservice对于我来说一直都是高大上的感觉,一个java web和javase都没 ...
- codeforces 558E A Simple Task 线段树
题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...
- VS链接数据库
可以用VS链接数据库,并进行数据库操作.前提是安装了Sqlserver,活着连接线上的数据库服务器.
- CloudFoundry 云平台部署
CloudFoundry云平台部署 CloudFoundry(TheOpenSourceCloudOperatingSystem)距离发布已经一年多了作为第一个开源的PaaS平台日臻成熟.在这一年里C ...