您的位置:首页 > 其它

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

2017-09-13 00:25 295 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: