【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] 总共两次询 ...
随机推荐
- 从Chrome源码看audio/video流媒体实现一(转)
现在绝大多数的网站已经从flash播放器转向了浏览器原生的audio/video播放器,浏览器是如何加载和解析多媒体资源的,这对于web开发者来说是一个黑盒,所以很有必要看一下浏览器是怎么实现的,Ch ...
- 洛谷1726 上白泽慧音 tarjan模板
题目描述 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间 ...
- thymeleaf 教程
html页面 添加 <html xmlns:th="http://www.thymeleaf.org" > html原有标签都可以用thymeleaf标签替换 1.t ...
- 怎样查看电脑的IP地址
在DOW窗体 :cmd->ipconfig 见截图:
- CCNP路由实验之六 动态路由协议之IS-IS
CCNP路由实验之六动态路由协议之IS-IS 动态路由协议能够自己主动的发现远程网络.仅仅要网络拓扑结构发生了变化.路由器就会相互交换路由信息,不仅能够自己主动获知新添加的网络.还能够在当前网络 ...
- HTML——上中下布局
上中下布局是最主要的布局方式,本比如果用户屏幕分辨率为800*600像素. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3Vuc2h1bWlu/font ...
- Windows系统时间同步出错解决办法(w32tm /register按回车,可能是为了解决时间COM注册的问题)
有时候我们设置本地时间与Internet时间同步时,经常连接服务器time.windows.com超时,导致时间同步失败,解决办法如下: 利用快捷键"Win+R"调出运行框,输入: ...
- 机器学习(三) Jupyter Notebook, numpy和matplotlib的详细使用 (上)
工欲善其事,必先利其器.在本章,我们将学习和机器学习相关的基础工具的使用:Jupyter Notebook, numpy和matplotlib.大多数教程在讲解机器学习的时候,大量使用这些工具,却不对 ...
- SQL try catch处理
ALTER PROC usp_AccountTransaction @AccountNum INT, @Amount DECIMAL AS BEGIN BEGIN TRY --Start the Tr ...
- mysql分页小结
mysql.select('*').from('books') .join('cSessionInfo', 'books.openid', 'cSessionInfo.open_id') .limit ...