Codeforces 914 C 数位DP+暴力打表+思维
题意
给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数,
问从\(1\)到\(n\)一共有多少个特殊的数,答案对\(1e9+7\)取模。
分析
\(n\)最大为\(2^{1000}\),二进制表示中最多有\(1000\)个\(1\),所以\(n\)以内的数经过一次简化后将变为\(1000\)以内的数,我们可以暴力打表\(1000\)以内的数简化为\(1\)所需的最少次数,将求得的次数加\(1\)即为二进制中\(1\)的个数为\(x\)的数简化为\(1\)所需的最少次数为\(cnt[x]\),\(1\)这个数要特判,没特判wa了3发。。。
然后分情况讨论:
\(k=0\),答案为\(1\),只有\(1\)是经过\(0\)次简化到\(1\)的数
\(k=1\),答案为\(n\)的位数\(-1\),\(n\)除了第\(1\)位,其余每一位为\(1\)都是特殊的数
\(k>1\),直接数位dp,设\(dp[i][j]\)为枚举到第\(i\)位二进制中\(1\)的个数为\(j\),\(cnt[x]==k\)的数为特殊的数
Code
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=1e9;
const int mod=1e9+7;
const int maxn=1e5+10;
char s[1010];
int a[1010],cnt[1010],k;
ll dp[1010][1010];
ll dfs(int pos,int x,int limit){
if(pos==0) return cnt[x]==k;
if(!limit&&~dp[pos][x]) return dp[pos][x];
int up=limit?a[pos]:1;
ll ret=0;
for(int i=0;i<=up;i++){
ret+=dfs(pos-1,x+(i==1),limit&&i==a[pos]);
ret%=mod;
}
if(!limit) dp[pos][x]=ret;
return ret;
}
int solve(int x){
if(x==1) return 0;
int ret=0;
int cnt=0;
while(x){
if(x&1) cnt++;
x>>=1;
}
ret+=solve(cnt)+1;
return ret;
}
void init(int n){
for(int i=1;i<=n;i++){
cnt[i]=solve(i)+1;
}
}
int main(){
ios::sync_with_stdio(false);
init(1005);
memset(dp,-1,sizeof(dp));
cin>>s+1>>k;
int n=strlen(s+1);
for(int i=1;i<=n;i++){
a[n-i+1]=s[i]-'0';
}
if(k==1){
cout<<n-1;
}else if(k==0) cout<<1;
else cout<<dfs(n,0,1);
return 0;
}
Codeforces 914 C 数位DP+暴力打表+思维的更多相关文章
- HDU 5179 beautiful number (数位dp / 暴力打表 / dfs)
beautiful number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- CodeForces - 55D(数位dp,离散化)
题目来源:http://codeforces.com/problemset/problem/55/D Volodya is an odd boy and his taste is strange as ...
- Codeforces #55D (数位dp+离散化)
Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive ...
- [DP]数位DP总结
数位DP总结 By Wine93 2013.7 1.学习链接 [数位DP] Step by Step http://blog.csdn.net/dslovemz/article/details/ ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...
- Codeforces 914 C. Travelling Salesman and Special Numbers (数位DP)
题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...
- Codeforces 55D (数位DP+离散化+数论)
题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...
- BZOJ 4513: [Sdoi2016]储能表 [数位DP !]
4513: [Sdoi2016]储能表 题意:求\[ \sum_{i=0}^{n-1}\sum_{j=0}^{m-1} max((i\oplus j)-k,0) \] 写出来好开心啊...虽然思路不完 ...
随机推荐
- 使用django的admin的后台管理界面
django的admin后台管理界面是方便我们对数据库操作的 是一个在浏览器显示的 图形化界面数据库操作 我们先在django中的admin中把我们需要在图形化界面中进行操作的表导入进去: 先把m ...
- Linux 系统必须掌握的文件_【all】
0.Linux 系统文件的详解 1.Linux 系统的网络配置文件 2.Linux 系统的DNS配置文件 3.Linux 系统的IP与域名解析文件[局域网的DNS] 4.Linux 系统的主机别名文件 ...
- 铁乐学python-面向对象的更多说明
以下内容摘自授课老师的博客http://www.cnblogs.com/Eva-J/ 面向对象的更多说明 面向对象的软件开发 很多人在学完了python的class机制之后,遇到一个生产中的问题,还是 ...
- python 统计学的各种检验
1.使用python中的Numpy进行t检验 http://www.atyun.com/7476.html 2.scipy中的卡方检验 http://wiki.mbalib.com/wiki/%E5% ...
- blank site teamplate去了哪里?
在sharepoint 2010包括sharepoint2010以前,有一个模板是blank site template.到了sharepoint2013,突然发现没有了. 再也不能生成基于blank ...
- November 15th 2016 Week 47th Tuesday
Success is finding satisfaction in giving a little more than you take. 成功就是付出比得到多,仍然心满意足. Can I find ...
- centos 增加网卡
CentOS 6添加网卡的方法 (2013-11-26 17:19:44) 转载▼ 标签: it 分类: Linux 前段时间安装了1台XEN server虚拟机,之前只用了1个网卡,ip是10.11 ...
- 死磕salt系列-salt文章目录汇总
死磕salt系列-salt入门 死磕salt系列-salt配置文件 死磕salt系列-salt grains pillar 配置 死磕salt系列-salt 常用modules 死磕salt系列-sa ...
- [国家集训队]小Z的袜子
嘟嘟嘟 一眼就知道是莫队. 还不带修改,美滋滋. 按莫队的方法排序,然后用小学数学算一下概率,分子分母单独维护. #include<cstdio> #include<iostream ...
- (第二章)改善JavaScript,编写高质量代码。
建议34:字符串是非值操作 var a = "javascript"; var b = a; b = b.toUpperCase(); alert(a); //javascript ...