POJ 3971 Scales

Description

You are given scales for weighing loads. On the left side lies a single stone of known weight W<2  N. You own a set of N different weights, weighing 1, 2, 4 ... 2  (N-1) units of mass respectively. Determine how many possible ways there are of placing some weights on the sides of the scales; so as to balance them (put them in a state of equilibrium). Output this value modulo a small integer D.

Input

The input begins with an integer t, the number of test cases. Then t test cases follow. For each test case, the first line contains three integers: N L D, where N denotes the number of weights at your disposal, L is the length of the binary representation of number W, and D is the modulus (1<=L<=N<=1000000, 2<=D<=100). The second line contains the value of W, encoded in the binary system as a sequence of exactly L characters 0 or 1 without separating spaces.

Output

For each test case, output a single line containing one integer - the calculated number of possible weight placements, modulo D.

Sample Input

2
6 4 6
1000
6 6 100
100110

Sample Output

3

5

动规,考虑是否进位的问题。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1000005;
int n,l,d,f[maxn][2],s[maxn];
char c[maxn];

int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		memset(f,0,sizeof(f));
		memset(s,0,sizeof(s));
		scanf("%d%d%d",&n,&l,&d);
		scanf("%s",c);
		for (int i=l-1;i>=0;i--) s[l-i-1]=c[i]-'0';
		f[0][0]=1;	f[0][1]=s[0];

		for (int i=1;i<n;i++)
		{
			if (s[i]==0)
			{
				f[i][0]=f[i-1][0]+f[i-1][1];
				f[i][1]=f[i-1][1];
			}
			else 
			{
				f[i][0]=f[i-1][0];
				f[i][1]=f[i-1][0]+f[i-1][1];
			}
			f[i][0]%=d;
			f[i][1]%=d;
		}
		printf("%d\n",(f[n-1][0]+d)%d);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值