Codeforces - 914C 数位DP
题意有点难以描述,简略的就是给定一个二进制\(n\),每一步操作能使\(n\)的位为1的数的和转化为一个十进制,然后转化为该数的二进制再进行相同的操作
查询\([0,n]\)中操作数恰好为\(k\)的使该数最终降为1的个数
注意,\(1_{(2)}\)的操作数是0
n的范围非常大可以想到是数位DP,并且它递减得非常快所以k其实连10都不到就不需统计了
Update:发现代码部分地方写sb了,不过影响不大
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int MAXN = 5e5+11;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
typedef long long ll;
const ll MOD = 1e9+7;
ll read() {
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int k;
char str[MAXN];
int a[MAXN],f[MAXN];
ll dp[1007][1007][11];
int F(int x){ // x(10)->1(2)
if(x<=1) return 0;
if(~f[x]) return f[x];
int cnt=0;
while(x){
cnt+=(x&1);
x>>=1;
}
return f[x]=1+F(cnt);
}
ll DP(int cur,int _1,int k,bool limit){
if(cur==0) return F(_1)==k-1;
if(!limit&&dp[cur][_1][k]!=-1) return dp[cur][_1][k];
int up=limit?a[cur]:1;
ll ans=0;
rep(i,0,up){
ans+=DP(cur-1,_1+i,k,limit&&a[cur]==i)%MOD;
ans%=MOD;
}
return limit?ans:dp[cur][_1][k]=ans;
}
ll solve(){
int len=strlen(str+1);
rep(i,1,len) a[i]=str[len-i+1]-'0';
return DP(len,0,k,1);
}
int main(){
memset(f,-1,sizeof f);
memset(dp,-1,sizeof dp);
while(~scanf("%s",str+1)){
k=read();
if(k>10){
println(0);
}else if(k==1){
ll tmp=solve();
tmp=((tmp-2)%MOD+MOD)%MOD;
println(tmp);
}else if(k==0){
println(1);
}
else{
println(solve());
}
}
return 0;
}
Codeforces - 914C 数位DP的更多相关文章
- Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)
大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...
- Codeforces 55D (数位DP+离散化+数论)
题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...
- Codeforces 628D 数位dp
题意:d magic number(0<=d<9)的意思就是一个数,从最高位开始奇数位不是d,偶数位是d 题目问,给a,b,m,d(a<=b,m<2000)问,a,b之间有多少 ...
- codeforces 401D (数位DP)
思路:很明显的数位dp,设dp[i][j] 表示选取数字的状态为i,模m等于j的数的个数,那么最后的答案就是dp[(1<<n)-1][0].状态转移方程就是,dp[i|(1<< ...
- codeforces 55D 数位dp
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Shovel Sale CodeForces - 899D (数位dp)
大意: n把铲子, 价格1,2,3,...n, 求有多少个二元组(x,y), 满足x+y末尾数字9的个数最多. 枚举最高位, 转化为从[1,n]中选出多少个二元组和为$x$, 枚举较小的数 若$n\g ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Gym 100231L Intervals 数位DP
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...
随机推荐
- [SoapUI] 通过Groovy获取SoapUI当前Project所在的目录
import com.eviware.soapui.support.GroovyUtils def groovyUtils = new GroovyUtils( context ) def proje ...
- 基于mosquitto的MQTT服务器---SSL/TLS 单向认证+双向认证
基于mosquitto的MQTT服务器---SSL/TLS 单向认证+双向认证 摘自:https://blog.csdn.net/ty1121466568/article/details/811184 ...
- hdu 4946 Area of Mushroom (凸包,去重点,水平排序,留共线点)
题意: 在二维平面上,给定n个人 每个人的坐标和移动速度v 若对于某个点,只有 x 能最先到达(即没有人能比x先到这个点或者同时到这个点) 则这个点称作被x占有,若有人能占有无穷大的面积 则输出1 , ...
- 【#】Spring3 MVC 注解(二)---@RequestMapping
博客分类: spring MVC 1 问题:有多个 @RequestMapping @controller @RequestMapping("/aaa") ...
- 14、Semantic-UI之菜单样式
14.1 基础菜单样式 在Semantic-UI中使用class="ui menu". 示例:定义基础菜单样式 <div class="ui menu" ...
- memcached整理の内存管理及删除机制
内存的碎片化 如果用C语言直接malloc,free来向操作系统申请和释放内存时,在不断申请和释放的过程中,形成了一些很小的内存片段,无法再利用.这种空闲但无法利用内存的现象称为内存的碎片化. sla ...
- Panda3d code in github
https://github.com/panda3d/panda3d [ref files] http://www.panda3d.noie.name/ https://www.panda3d.org ...
- Tomcat 警告:consider increasing the maximum size of the cache
最近在Tomcat8上导入原本Tomcat6的项目,报了以下错误:Tomcat 警告:consider increasing the maximum size of the cache. 这是因为to ...
- Backup--如何快速截断日志
--在SQL Server 2005 中,可用使用 BACKUP LOG WITH TRUNCATE_ONLY来迅速清理日志,该命令在 SQL Server2008 及更高版本上被去除. --BACK ...
- C#文件和目录的操作
根据文件名获取文件 /// <summary> /// 根据文件名获取文件 /// </summary> /// <param name="directory& ...