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, ...
随机推荐
- 斐波那契数列—java实现
最近在面试的时候被问到了斐波那契数列,而且有不同的实现方式,就在这里记录一下. 定义 斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
- SQL2000中创建作业(定时查询,更新)(转)
出处:http://blog.csdn.net/xys_777/article/details/5683413 SQL2000中创建作业(定时查询,更新)企业管理器 --管理 --SQL Server ...
- 前端福利之改变placeholder颜色的方法(转)
之前拿到一个设计图,Placeholder是白色的,所以就查看了一下改变placeholder的方法: input::-webkit-input-placeholder { /* WebKit bro ...
- 在Lua中封装一个调试日志(附lua时间格式)
--自己封装一个Debug调试日志 Debug={} Info={} local function writeMsgToFile(filepath,msg) end function Debug.Lo ...
- 使用 console.time() 计算js代码执行时间
console.time('hellor'); for(var i=0;i<100000;i++){} console.timeEnd('hellor');
- select、poll、epoll的比较
linux提供了select.poll.epoll接口来实现IO复用,三者的原型如下所示,本文从参数.实现.性能等方面对三者进行对比. int select(int nfds, fd_set *rea ...
- Gym - 100989H (贪心)
After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...
- ibatis源码学习3_源码包结构
ibatis的技术是从xml里面字符串转换成JAVA对象,对象填充JDBC的statement查询,然后从resultset取对象返回,另外利用ThreadLocal实现线程安全,JDBC保证了事务控 ...
- DateTime.Now.ToString("yyyy/MM/dd") 时间格式化中的MM为什么是大写的?
如果MM是小写,就表示时间里的分钟yyyy-MM-dd HH:mm:ss (年-月-日 时:分:秒) yyyy-MM-dd HH:mm:ss 年-月-日 时:分:秒大写是为了区分“月”与“分” 顺便说 ...
- 按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式
按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式 之前写过一篇文章<按照已有的模板输出一(如发票)>,是关于如何给已有的模板赋值.在项目的实践过程 ...