当前位置:编程学习 > C/C++ >>

CF problem 166E Tetrahedron


找规律, f[n]跟f[n-1]和f[n-2]明显有关。。。


 

?#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<string>  
#include<stack>  
#include<queue>  
#include<map>  
#include<set>  
#include<cstring>  
#include<cstdio>  
#include<cstdlib>  
 
using namespace std; 
 
#define FF(i, a, b) for(i=a; i<b; i++)  
#define FD(i, a, b) for(i=a; i>b; i--)  
#define CLR(a, b) memset(a, b, sizeof(b))  
#define CPY(a, b) memcpy(a, b, sizeof(a))  
 
long long n; 
long long f[10000001]; 
 
int main() 
{ 
    f[1] = 0; 
    f[2] = 3; 
    f[3] = 6; 
    int i; 
    FF(i, 4, 10000001) 
    { 
        f[i] = f[i-1] * 2 + f[i-2] * 3; 
        f[i] %= 1000000007; 
    } 
    while(cin>>n) cout<<f[n]<<endl; 
    return 0; 
} 

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstring>
#include<cstdio>
#include<cstdlib>

using namespace std;

#define FF(i, a, b) for(i=a; i<b; i++)
#define FD(i, a, b) for(i=a; i>b; i--)
#define CLR(a, b) memset(a, b, sizeof(b))
#define CPY(a, b) memcpy(a, b, sizeof(a))

long long n;
long long f[10000001];

int main()
{
    f[1] = 0;
    f[2] = 3;
    f[3] = 6;
    int i;
    FF(i, 4, 10000001)
    {
        f[i] = f[i-1] * 2 + f[i-2] * 3;
        f[i] %= 1000000007;
    }
    while(cin>>n) cout<<f[n]<<endl;
    return 0;
}

 

 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,