BZOJ3329 Xorequ(数位dp+矩阵快速幂)

本文解析了一道BZOJ上的权限题目,该题要求求解特定数学表达式的解的数量,并给出了两种不同的子任务及其解决方案。子任务1采用数位动态规划的方法解决,而子任务2则通过观察规律发现解的数量符合斐波那契数列的特点,进而利用矩阵快速幂进行高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Input

第一行一个正整数,表示数据组数据 ,接下来T行
每行一个正整数N

Output

2*T行
第2*i-1行表示第i个数据中问题一的解,

第2*i行表示第i个数据中问题二的解,

Sample Input

1
1

Sample Output

1
2

HINT

x=1与x=2都是原方程的根,注意第一个问题的解
不要mod 10^9+7

1<=N<=10^18

1<=T<=1000


这在BZOJ上是一道权限题,但是一道很不错的题,这里拿来讲一下

对于题意中的 x ^ 3x == 2x 可以等价于 x ^ 2x == 3x

那么 3x = x + 2x

所以我们就需要找出 x ^ 2x == x + 2x的数字个数

因为 a + b == (a ^ b) + (a & b) << 1这个大家手动模拟一下就发现是很明显的

那么我们就知道 (x & 2x) == 0 才能满足原来条件,那么意思就是 x 在二进制下不能有相邻的 1

对于 subtask1 我们就可以直接数位dp了

对于 subtask2 我们打表找规律可得 答案就是斐波那契数列的第 (n + 2) 项,那么我们就可以用矩阵快速幂快速算出答案了


详情见AC代码:

/*************************************************************************
	 > Author: wzw-cnyali
	 > Created Time: 2017/9/12 20:26:07
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>

#prag\
ma GCC optimize("O3")

using namespace std;

typedef long long LL;

typedef unsigned long long uLL;

#define REP(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define DREP(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define mem(a, b) memset((a), b, sizeof(a))

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

template <class T>
T read(T sum = 0, T fg = 0)
{
	char c = getchar();
	while(c < '0' || c > '9') { fg |= c == '-'; c = getchar(); }
	while(c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); }
	return fg ? -sum : sum;
}

const LL mod = 1e9 + 7;

const int Size = 70;

int a[Size], len;

LL dp[Size][12];

LL dfs(int pos, int pre, int lim)
{
	if(pos == -1) return 1;
	if(!lim && dp[pos][pre] != -1) return dp[pos][pre];
	LL res = 0;
	REP(i, 0, lim ? a[pos] : 1)
	{
		if(pre == 1 && i == 1) continue;
		res += dfs(pos - 1, i, lim && i == a[pos]);
	}
	if(!lim) dp[pos][pre] = res;
	return res;
}

LL solve1(LL x)
{
	len = 0;
	do { a[len++] = x % 2; x /= 2; }while(x);
	return dfs(len - 1, 11, 1) - 1;
}

struct Matrix
{
	LL matrix[5][5];
	int lenx, leny;

	Matrix(int a = 0)
	{
		lenx = 2; leny = 2;
		if(a == 0) matrix[1][1] = matrix[1][2] = matrix[2][1] = matrix[2][2] = 0;
		else
		{
			matrix[1][1] = matrix[1][2] = matrix[2][1] = 1;
			matrix[2][2] = 0;
		}
	}

	void base()
	{
		matrix[1][1] = matrix[2][2] = 1;
		matrix[1][2] = matrix[2][1] = 0;
	}

	friend Matrix operator * (Matrix a, Matrix b)
	{
		Matrix res;
		res.lenx = a.lenx; res.leny = b.leny;
		REP(i, 1, res.lenx) REP(j, 1, res.leny) REP(k, 1, res.leny)
			(res.matrix[i][j] += (a.matrix[i][k] * b.matrix[k][j]) % mod) %= mod;
		return res;
	}
};

Matrix qpow(Matrix a, LL N)
{
	Matrix ans; ans.base();
	while(N)
	{
		if(N & 1LL) ans = ans * a;
		a = a * a;
		N >>= 1LL;
	}
	return ans;
}

LL solve2(LL x)
{
	Matrix a = 1, b;
	Matrix ans = qpow(a, x + 1);
	b.lenx = 2; b.leny = 1;
	b.matrix[1][1] = b.matrix[1][2] = 1;
	ans = ans * b;
	return ans.matrix[1][1];
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	mem(dp, -1);
	int Case = read<int>();
	while(Case--)
	{
		LL x = read<LL>();
		printf("%lld\n", solve1(x));
		printf("%lld\n", solve2(x));
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值