codeforces 166E Tetrahedron 动态规划

题目链接 http://codeforces.com/problemset/problem/166/E

题意,一只蚂蚁在一个正四面体上行走。正四面体的每条边长度为1,假设蚂蚁刚开始在一个顶点上,且蚂蚁只在棱上走。问蚂蚁恰好走完长度为n距离,且回到起点的走法有多少种,结果可能很大,可以对1e9+7取模。

题目思路。

我们如果假设四个点为A,B,C,D。且假设蚂蚁刚开始在A点。

那么易得。当n=1时,走到B的可能情况有1种,走到C的可能情况有1种,走到D的可能情况有1种。走到A的可能情况为0种。

当n=2时,走到B的情况,必然为n等于1时,A,C,D的情况的总和。其他的一样。

所以就找到状态转移方程。

设dpA[i]为走长度为i到A点的可能情况。

再设dpB[i],dpC[i],dpD[i]。

那么dpA[i] = dpB[i-1]+dpC[i-1]+dpD[i-1];以此类推。

因为n很大,有10的7次方。开long long容易爆内存。

只要开int数组,小心的取模就可以了。

代码如下:

#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long LL;
typedef long long ll; 
const int maxn = 1e7+6;
const int modn = 1e9+7;
const int INF = 0x3f3f3f3f;

int a[maxn];
int d[maxn];
void show(int a[],int n){
	for(int i=0;i<n;i++){
		printf("%d ",a[i]);
	}printf("\n");
}
int main(){
//	freopen("C:\\Users\\lenovo\\Desktop\\data.in","r",stdin);
	int n;
	ll a1,d1,a2,d2;
	a[1]=1;
	d[1]=0;
	while(scanf("%d",&n)!=EOF){
		for(int i=2;i<=n;i++){
			a[i] = (2*a[i-1])%modn;
			a[i] = (a[i]+d[i-1])%modn;
			d[i] = (a[i-1]+a[i-1])%modn;
			d[i] =  (d[i]+a[i-1])%modn;
			
			
		}
		printf("%d\n",d[n]);
		
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_25955145/article/details/81268855