[CQOI2018]九连环

嘟嘟嘟


对于这种找规律的题,我向来是不会的。


通过大佬们的各种打表找规律、神奇dp等方法,我们得到了答案就是\(\lfloor \frac{2 ^ {n + 1}}{3} \rfloor\)
高精是显然的,但是还得用fft,毕竟这是省选题。


刚开始我一运行就RE,都不让你输入,后来才发现是数组开到1e6太大了(这怎么就大了!?)
其次别忘了高精里面的数都是倒着存的,所以做除法的时候得倒着来,最后再把数组倒过来。
然后高精fft借鉴了一下兔哥的代码,把原来的代码简化了许多。

#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 In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const db PI = acos(-1);
const int maxn = 1e5 + 5;
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 rev[maxn];
struct Comp
{
  	db x, y;
  	In Comp operator + (const Comp& oth)const
  	{
		return (Comp){x + oth.x, y + oth.y};
  	}
  	In Comp operator - (const Comp& oth)const
  	{
    	return (Comp){x - oth.x, y - oth.y};
  	}
  	In Comp operator * (const Comp& oth)const
  	{
    	return (Comp){x * oth.x - y * oth.y, x * oth.y + y * oth.x};
  	}
  	friend In void swap(Comp& a, Comp& b)
  	{
    	swap(a.x, b.x); swap(a.y, b.y);
  	}
};

In void fft(Comp* a, int len, int flg)
{
	for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
	for(int i = 1; i < len; i <<= 1)
	{
		Comp omg = (Comp){cos(PI / i), sin(PI / i) * flg};
		for(int j = 0; j < len; j += (i << 1))
		{
			Comp o = (Comp){1, 0};
			for(int k = 0; k < i; ++k, o = o * omg)
			{
				Comp tp1 = a[k + j], tp2 = a[k + j + i] * o;
				a[k + j] = tp1 + tp2, a[k + j + i] = tp1 - tp2;
			}
		}
	}
}

struct Big
{
  	int a[maxn], len;
  	In void init() {Mem(a, 0); len = 0;}
  	In Big operator * (const Big& oth)const
  	{
  		static Comp A[maxn], B[maxn];
    	int Len = 1, lim = 0;
    	while(Len < len + oth.len - 1) Len <<= 1, ++lim;
    	for(int i = 0; i < Len; ++i)
    	{
    		A[i] = (Comp){i < len ? a[i] : 0, 0};		//这个很重要 
    		B[i] = (Comp){i < oth.len ? oth.a[i] : 0, 0};
    	}
    	for(int i = 0; i < Len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
	    fft(A, Len, 1); fft(B, Len, 1);
	    for(int i = 0; i < Len; ++i) A[i] = A[i] * B[i];
	    fft(A, Len, -1);
	    static Big ret; ret.init(); ret.len = len + oth.len - 1;
	    for(int i = 0; i < ret.len; ++i) ret.a[i] = (int)(A[i].x / Len + 0.5);
	    for(int i = 0; i < ret.len; ++i) ret.a[i + 1] += ret.a[i] / 10, ret.a[i] %= 10;
		if(ret.a[ret.len]) ++ret.len;	//因为最多只会进一位,所以就不用while啦 
    	return ret;
	}
	In Big operator / (int x)
	{
		static Big ret; ret.init();
		for(int i = len - 1, tp = 0; i >= 0; --i)
		{
			tp = tp * 10 + a[i];
			if(ret.len) ret.a[ret.len++] = tp / x;
			else if(tp >= x) ret.a[ret.len++] = tp / x;
			tp %= x;
		}	
		reverse(ret.a, ret.a + ret.len);
		return ret;
	}
	In void out()
	{
		for(int i = len - 1; i >= 0; --i) write(a[i]);
	}
}A, ret;

int main()
{
	int T = read();
	while(T--)
    {
    	int n = read() + 1;
      	A.init(); ret.init();
		A.len = ret.len = 1;
	    A.a[0] = 2; ret.a[0] = 1;
	    for(; n; n >>= 1, A = A * A)
			if(n & 1) ret = ret * A;
		ret = ret / 3;
      	ret.out(), enter;
    }
  	return 0;
}
posted @ 2019-02-14 20:06  mrclr  阅读(257)  评论(0编辑  收藏  举报