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开始,自上而下计 ...
随机推荐
- maven课程 项目管理利器-maven 3-1 maven常用的构建命令
mvn -v 查看mvn版本 mvn compile 编译 mvn test 测试 mvn package 编译,打包(这个命令会在你的项目路径下一个target目录,并且拥有compile命令的功 ...
- 安全漏洞 : XSS CSRF
https://my.oschina.net/hc24/blog/527099 XSS成因概括 : XSS其实就是Html的注入问题,攻击者A的输入没有经过严格的控制进入了数据库,最终显示给来访的用户 ...
- JSON 常量详情参考 (内含对中文不转义的参数)
JSON_ERROR_NONE (integer) 没有错误发生.自 PHP 5.3.0 起生效. JSON_ERROR_DEPTH (integer) 到达了最大堆栈深度.自 PHP 5.3.0 起 ...
- centos7 & centos6 rrdcache
cat > /etc/systemd/system/rrdcached.service << EOF [Unit] Description=Data caching daemon f ...
- Description Resource Path Location Type Java compiler level does not match the version of the installed Java project facet Unknown Faceted Project Problem (Java Version Mismatch)
project 编译问题,需要三处的jdk版本要保持一致,才能编译通过. 1.在项目上右键properties->project Facets->修改右侧的version 保持一致 2. ...
- linux基础命令-文件查看类命令cat/tac/more/less/tail/head
cat 查看文本文件内容 -b :给每一行编号不包括空白行 -n:给所有行编号 [root@host01 tmp]# cat -An /root/anaconda-ks.cfg # Kickstart ...
- Django基础--3
ORM django一对多的操作 获取表单的三种方式:views 获取对象 .all() html中获取方式为 row.id,row,host 获取字典 ...
- Altium_Designer-怎么将“原理图的更改”更新到“pcb图”?
打开原理图,直击菜单栏>>Design,选择第一项,>>Update PCB Document...在弹出的对话框里面选择执行更改即可将原理图更新到工程下面对应的PCB.也可以 ...
- C#后台unxi时间戳转换为前台JS时间的方法
后台返回的时间是一个格式为 /Date(1530153274362)/ 的unxi时间戳前台转换代码:var matchResult = data.match(/(\d+)/);if (matchRe ...
- CentOS 6\7修改主机名
1.CentOS6修改主机名 1)临时修改主机名: 显示主机名: oracle@localhost:~$ hostname localhost 修改 oracle@localhost:~$ sudo ...