Codeforces Round #458C DP
1 second
256 megabytes
standard input
standard output
The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pass time, he likes to perform operations on numbers. One such operation is to take a positive integer x and reduce it to the number of bits set to 1 in the binary representation of x. For example for number 13 it's true that 1310 = 11012, so it has 3 bits set and 13 will be reduced to 3 in one operation.
He calls a number special if the minimum number of operations to reduce it to 1 is k.
He wants to find out how many special numbers exist which are not greater than n. Please help the Travelling Salesman, as he is about to reach his destination!
Since the answer can be large, output it modulo 109 + 7.
The first line contains integer n (1 ≤ n < 21000).
The second line contains integer k (0 ≤ k ≤ 1000).
Note that n is given in its binary representation without any leading zeros.
Output a single integer — the number of special numbers not greater than n, modulo 109 + 7.
110
2
3
111111011
2
169
In the first sample, the three special numbers are 3, 5 and 6. They get reduced to 2 in one operation (since there are two set bits in each of 3, 5 and 6) and then to 1 in one more operation (since there is only one set bit in 2).
代码:
//#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e3 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std; int f[N],a[N],C[N][N];
int n,m,ans,tot;
char str[N]; int calc(int x)
{
int res=;
while(x){if(x&)res++;x>>=;}
return res;
} void work(int x)
{
int cnt=;
for(int i=n;i>=;i--)//x个1放在n个位置上
{
if(a[i])//当原数字对应位为1时,可以放1与0。
{
if(x>=cnt) ans=(ans+C[i-][x-cnt])%mod;// 此位不放1的种数
cnt++;//之后此位放1
}
}
if(x==tot) ans=(ans+)%mod;
} int main()
{
scanf("%s%d",str+,&m);n=strlen(str+);
for(int i=;i<=n;i++) a[i]=str[i]-'',tot+=a[i];
reverse(a+,a++n);
for(int i=;i<=n;i++)
{
C[i][]=;
for(int j=;j<=i;j++) C[i][j]=(C[i-][j]+C[i-][j-])%mod;
}
f[]=;
for(int i=;i<=;i++) f[i]=f[calc(i)]+;
if(m==) puts("");
else
{
for(int i=;i<=;i++) if(f[i]==m-) work(i);
if(m==) ans--;
printf("%d\n",(ans+mod)%mod);
}
return ;
}
Codeforces Round #458C DP的更多相关文章
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #205 (Div. 2)C 选取数列可以选择的数使总数最大——dp
http://codeforces.com/contest/353/problem/C Codeforces Round #205 (Div. 2)C #include<stdio.h> ...
- Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...
- Codeforces Round #267 (Div. 2) C. George and Job(DP)补题
Codeforces Round #267 (Div. 2) C. George and Job题目链接请点击~ The new ITone 6 has been released recently ...
- 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...
- 树形DP Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
题目传送门 /* 题意:求一个点为根节点,使得到其他所有点的距离最短,是有向边,反向的距离+1 树形DP:首先假设1为根节点,自下而上计算dp[1](根节点到其他点的距离),然后再从1开始,自上而下计 ...
随机推荐
- canvas制作倒计时效果
- 好用的切换滑动焦点图框架jquery.superslide
拿到学习网站:http://www.superslide2.com/
- Linux centos6.x 配置免密码登录
免密码登录主要就是被访机器提供公匙给访问者,然后访问者使用ssh协议时可以使用所配置好的公匙验证.这样就免去了输入密码的麻烦. 某些集群例如hadoop,一般都需要将主机和其他机器间配置无密码公匙认证 ...
- Struts2_访问Web元素
取得Map 类型的 request,session,application, HttpServletRequest,HttpSession,ServletContext的引用. 分访问 Map 类型和 ...
- Markdown快速上手指南
Markdown快速上手指南 1.Markdown介绍 markdown可以实现快速html文档编辑,格式优没,并且不需要使用html元素. markdown采用普通文本的形式,例如读书笔记等易于使用 ...
- 一种轻量级的C4C业务数据同步到S4HANA的方式:Odata通知
SAP Cloud for Customer和SAP其他传统产品的同步,除了使用SAP Netweaver Process Integration和SAP HANA Cloud Integration ...
- Excel公式巧用-将新值替换旧值,新值为空保留原值
使用excel时候遇到 将新值替换旧值,新值为空保留原值的问题,简单使用Excel的函数即可以实现.
- selenium项目--读取测试用例
读取测试用例 一直我们都没有考虑过读取测试用例的事,我们现在这样设计测试用例有两个好的点,在执行方法时,打印测试用例,方便知道执行的内容是什么,在报告展示时,把测试用例的结果展示出来 实现方案:目前我 ...
- 问答 请问使用OK("raw:jpg")能返回多张图片吗
请问使用OK("raw:jpg")能返回多张图片吗 发布于 28天前 作者 qq_3aeeb0ad 78 次浏览 复制 上一个帖子 下一个帖子 标签: 无 @At( ...
- Cocos2d-x手机游戏开发必备C++语言基础
http://edu.51cto.com/course/course_id-1380-page-1.html