bzoj3329 Xorequ

嘟嘟嘟


第一问是数位dp。因为我既不会数位dp也不会记搜,所以就抄了份儿代码。这个坑有时间一定要填。
第二问矩乘优化斐波那契。
没了。
(好像啥也没讲)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e3 + 5;
const ll mod = 1e9 + 7;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int num[maxn], cnt = 0;
ll dp[maxn][2];
ll dfs(int x, bool lim, bool pre)
{
  if(!lim && dp[x][pre] != -1) return dp[x][pre];
  if(!x) return 1;
  ll ans = 0;
  if(!pre && (num[x] == 1 || !lim)) ans += dfs(x - 1, lim, 1);
  ans += dfs(x - 1, lim && !num[x], 0);
  if(!lim) dp[x][pre] = ans;
  return ans;
}
ll solve1(ll n)
{
  cnt = 0;
  while(n) num[++cnt] = (n & 1), n >>= 1;
  return dfs(cnt, 1, 0);
}

const int N = 2;
struct Mat
{
  ll a[N][N];
  Mat operator * (const Mat& oth)const
  {
    Mat ret; Mem(ret.a, 0);
    for(int i = 0; i < N; ++i)
      for(int j = 0; j < N; ++j)
	for(int k = 0; k < N; ++k)
	  ret.a[i][j] += a[i][k] * oth.a[k][j], ret.a[i][j] %= mod;
    return ret;
  }
}F, A;
void init()
{
  Mem(F.a, 0);
  F.a[0][0] = F.a[0][1] = F.a[1][0] = 1;
}
Mat quickpow(Mat A, ll b)
{
  Mat ret; Mem(ret.a, 0);
  for(int i = 0; i < N; ++i) ret.a[i][i] = 1;
  for(; b; b >>= 1, A = A * A)
    if(b & 1) ret = ret * A;
  return ret;
}
ll solve2(ll n)
{
  init();
  A = quickpow(F, n);
  return (A.a[0][0] + A.a[0][1]) % mod;
}

int main()
{
  Mem(dp, -1);
  int T = read();
  while(T--)
    {
      ll n = read();
      write(solve1(n) - 1), enter;
      write(solve2(n)), enter;
    }
  return 0;
}
posted @ 2018-12-14 16:16  mrclr  阅读(93)  评论(0编辑  收藏  举报