cf914C(数位DP)

有点感动..竟然当场独立做出了数位DP..感觉假期这个或许可以练得起来?

看到要直接统计各数的变换次数绝对是不现实的..可是如果手动先变换一次的话就变成1000以内了,这样就可以在统计变换次数的基础上+1就可以了..

那么剩下的就是把符合题目的次数的数给挑出来了,其实很明显变换次数和1的个数有关,所以有必要把1个数的相同的归类,所以并没有什么特别正式的转移方程,处理1个数统计

对各位数扫一遍,如果是0就留给下一位,如果是1,那么先令它为0,后面的数就可以随便取了,枚举1的个数加上前面的1,配合预处理的变换次数看是不是能等于k,能的话加上对应的方案数,方案数其实就是组合数(在几个数位里面去若干一个1),最后在判断这个数本身是否符合要求即可..

当然组合数是可以预处理出来的..用杨辉三角就可以...当时智障想了半天(砸

另外k==0的情况需要预判,上面都默认变换了至少一次的..

然而还是fft了...原因是1只变换了0次,而在算的时候已经是当作做过一次变换处理,所以k==1的时候需要把这个1减掉


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1000000007
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls (T+i*2)
#define rs (T+i*2+1)
#define op (T+i)
#define mid (x+y>>1)
#define NM 1005
#define nm 2000005
#define pi 3.141592653
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}



int n,k,m,tot;
ll c[NM],d[NM][NM],ans;
char s[NM];

int main(){
	//freopen("data.in","r",stdin);
	scanf("%s",s);
	n=strlen(s);
	k=read()-1;
	if(k==-1){printf("1\n");return 0;}
	inc(i,2,n){
		int s=0;
		for(int t=i;t;t/=2)s+=t%2;
		c[i]=c[s]+1;
	}
	//inc(i,1,n)printf("%d ",c[i]);putchar('\n');
	d[0][0]=1;
	d[1][0]=1;d[1][1]=1;
	inc(i,2,n){
		d[i][0]=d[i][i]=1;
		inc(j,1,i-1)d[i][j]=(d[i-1][j]+d[i-1][j-1])%inf;
	}
	//inc(i,1,n)printf("%d\n",d[n][i]);
	inc(i,1,n)if(s[i-1]=='1'){
		inc(j,0,n-i)if(j+tot>0&&c[j+tot]==k)
			(ans+=d[n-i][j])%=inf;
		tot++;
		//printf("%d\n",ans);
	}
	ans+=c[tot]==k;ans%=inf;
	ans-=k==0;
//	printf("%d\n",tot);
	return 0*printf("%I64d\n",ans);
}



C. Travelling Salesman and Special Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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

Output a single integer — the number of special numbers not greater than n, modulo 109 + 7.

Examples
Input
110
2
Output
3
Input
111111011
2
Output
169
Note

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).


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值