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开始,自上而下计 ...
随机推荐
- PHP Regex
<?php //Accpet the http client request and generate response content. //As a demo, this function ...
- Refactoring in Coding
Make changes on existing code for subsequent and constant changes of requirement. Reference:http://w ...
- Struts2_Action
具体视图的返回可以由用户自己定义的Action来决定:具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容:具体Action的实现可以是一个普通的java类,里面有public String ...
- HTML5 data-* 自定义属性操作及其注意点
在HTML5中添加了data-*的方式来自定义属性,所谓data-*实际上上就是data-前缀加上自定义的属性名,命名可以用驼峰命名方式,但取值是必需全部使用小写(后面会说),使用这样的结构可以进行数 ...
- redis笔记(三)
redis配置文件 配置文件对单位大小写不敏感 tcp-backlog 511 高并发环境连接数 tcp-keepalive 单位为秒 0 表示不会进行keepalive检测,,,建议设置 ...
- xampp中mysql重置root密码
1. 停止mysql:用图形化工具或者在cmd命令下输入net stop mysql,在c盘根目录下输入 2. 打开cmd,切换目录到 /xampp/mysql/bin, 运行 mysqld ...
- 【微软大法好】VS Tools for AI全攻略(4)——选择适合自己的虚拟机
当我们选择好了自己的虚拟机后,也许效果不尽如人意.就比如我,发现代码在训练一段时间之后,CPU的使用率会下降. 这个时候我们就要开始考虑,是不是我们选择的虚拟机不是适合自己的型号. Azure的虚拟机 ...
- 《孵化Twitter》:Twitter创始人勾心斗角史,细节披露程度令人吃惊
本书详细讲述twitter的发展史.感觉基本上是一部创始人从朋友变敌人,勾心斗角的历史.Twitter本身的产品发展反而相对比较简单. 书中披露了许多email.谈话.会议的细节,作者说这些是数百个小 ...
- 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...
- 【转载】#437 - Access Interface Members through an Interface Variable
Onece a class implementation a particular interface, you can interact with the members of the interf ...