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开始,自上而下计 ...
随机推荐
- elasticsearch复杂查询-----2
1.多条件查询 1)查询索引weibo下字段date大于或等于2015-09-05和name为Mary Jone的数据 2.简单查询 1)查询包含2014字符的数据 2)查询包含字符2014-09-1 ...
- 弹性布局 Flexible Box
页面中任何一个元素都可以指定为 弹性布局(Flex) 属性:display 取值: 1.flex 将块级元素变为弹性布局容器 2.inline-flex 将行内元素变为弹性布局容器 兼容性 ...
- Kindeditor单独调用多图上传
html代码: <input type="button" id="J_selectImage" value="图片上传" />& ...
- C++ Knowledge series 2
Programming language evolves always along with Compiler's evolvement The semantics of constructors O ...
- Android触摸事件MotionEvent详解
触摸事件MotionEvent在用户交互中,占着非常重要的地位.首先,来看看MotionEvent中封装的一些常用的事件常量,它定义了触摸事件的不同类型. 1.单点触摸按下动作 public stat ...
- 学习lucene5.5.4的笔记
说说几个常用的类. OpenMode是一个枚举类,有三个元素,分别表示IndexWriter的打开模式. CREATE:每次打开IndexWriter时清空当前索引目录下的索引,再新建索引. APPE ...
- Windows计算下载文件的SHA256 MD5 SHA1
引用自 http://blog.163.com/licanli2082@126/blog/static/35748686201284611330/ certutil -hashfile yourfil ...
- C++ new new[]详解
精髓: operator new()完成的操作一般只是分配内存:而构造函数的调用(如果需要)是在new运算符中完成的. operator new和new 运算符是不同的,operator new只分配 ...
- leetcode 136、Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single on ...
- git/github初级运用自如(转自:虫师)
注:本文来源于 虫师博客(http://www.cnblogs.com/fnng/archive/2012/01/07/2315685.html) ,内容详尽,真实有用. 另:发一个github使用教 ...