Pokémon GO
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 327 Accepted Submission(s): 187
今天它决定要抓住所有的精灵球!
为了不让度度熊失望,精灵球已经被事先放置在一个2*N的格子上,每一个格子上都有一个精灵球。度度熊可以选择任意一个格子开始游戏,抓捕格子上的精灵球,然后移动到一个相邻的至少有一个公共点的格子上继续抓捕。例如,(2, 2) 的相邻格子有(1, 1), (2, 1) 和 (1, 2) 等等。
现在度度熊希望知道将所有精灵球都抓到并且步数最少的方案数目。两个方案被认为是不同,当且仅当两个方案至少有一步所在的格子是不同的。
每组数据包含一个数N。
●1≤T≤100
●1≤N≤10000
3 1 2 3
2 24 96
思路:
名词解释相对的格子:一列之中,除了指定格子之外的另一个格子。
1、构造两个动态规划数组和一个计数器sum,一个数组a[x],表示在2*x的格子条件下,从最边缘一列的一个角的格子出发,遍历全体格子的种类数,显然a[1]=1,另一个数组b[x],表示在2*x的格子条件下,从一个角的格子出发,遍历全体格子后回到与之相对的格子的种类数。如图所示,显然因为要考虑到回来的路径,因此除了出发点之外,每一列都只有2种选择方法,因此b[x]=2*b[x-1]
2、先考虑出发点在角上的问题,从一个角出发,只有3种可能性,(1)那就是先去相对的格子,然后前往下一列,这就简化成为从2*x-1列的格子中,从一个角出发遍历所有格子的问题,因为前往下一列的第一个格子有两种选法,因此a[x]+=2*a[x-1];(2)第二种可能性就是先去遍历其余格子,最后以相对的格子收尾。此时a[x]+= b[x];(3)第三种可能性较为复杂,先经过第二列的一次转折,然后到第三列的一个角上进行遍历。此时第二列有2种选法,第三列有2种选法,因此a[x]+=4*a[x-2];
3、再去考虑出发点在中间的问题,如图所示,出发点在中间的时候,显然不能直接往下走,否则无法遍历所有点,应当是先遍历左边(右边)所有点,然后回到相对的点,然后遍历右边(左边)的点。注意先遍历的时候,必须是采用“遍历全体格子后回到与之相对的格子”的走法,否则无法遍历出发点正下方的点,而后遍历则不受限制。因此设从第i列开始出发,出发点有两种选法,第一落脚点又有两种走法,后遍历的第一落脚点又有两种走法,走完总走法数为2*(2*b[i-1]*2*a[n-i])+2*(2*b[n-i]*2*a[i-1]) (加法的前一半是先遍历左边,后一半是先遍历右边)
4、总走法数就是4*a[i](因为有4个角)+ 从2到第n-1列所有从中间走法数的和。
//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;
#define pi acos(-1)
#define PI acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
#define db double
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1e4+10;
const int maxx=1e6+100;
const double EPS=1e-8;
const double eps=1e-8;
const int mod=1e9+7;
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
template <class T>
inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}
while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}
inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}
else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}
if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}
if(IsN) num=-num;return true;}
void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}
void print(LL a){ Out(a),puts("");}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
//cerr << "run time is " << clock() << endl;
LL sum, t1[maxn], t2[maxn],n;
void init()
{
t1[1]=1;
for (int i=2; i<=maxn; i++)
{
t1[i]=(t1[i-1]*2%mod);
t1[i]%=mod;
}
t2[1]=1;
t2[2]=6;
for (int i=3; i<=maxn; i++)
{
t2[i]=(2*t2[i-1]+t1[i]+4*t2[i-2])%mod;
}
}
int main()
{
init();
int T;
scan_d(T);
W(T--)
{
scan_d(n);
if(n==1)
{
puts("2");
continue;
}
LL sum=4*t2[n];
for (int i=2; i<n; i++)
{
sum=(sum+(8*t2[i-1]*t1[n-i]%mod)%mod+(8*t2[n-i]*t1[i-1])%mod)%mod;
}
print(sum);
}
}