Codeforces Round #118 (Div. 2) C. Plant (找规律)

题目链接:点击打开链接

题意:问你第n个大三角形中有几个小三角形是朝上的。


思路:每个朝上的小三角形下一年会变成三个朝上一个朝下,每个向下的小三角下年会变成三个朝下一个朝上。


我们可以设第i年朝上的是ai个朝下的是bi个,因此可以得到递推式:ai = 3*ai-1 + bi-1, 

bi = 3*b*-1 + ai-1, 可以看到这个式子很像,两者做差得到ai-bi = 2*(ai-1 - bi-1),可得到

第i者之差的公式,又可知每年有4^i个三角,即ai + bi = 4^i, ai - bi = 2^n 两者作和

除2即为答案,快速幂即可解决。



代码:

#include
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll n;

ll p(ll a, ll k)
{
    a %= mod;
    ll ans = 1;
    while(k)
    {
        if(k&1) ans = ans*a%mod;
        a = a*a%mod;
        k /= 2;
    }
    return ans;
}

int main(void)
{
    while(cin >> n)
    {
        if(!n) puts("1");
        else printf("%I64d\n", (p(2, n-1)+p(2, 2*n-1))%mod);
    }
    return 0;
}



C. Plant
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dwarfs have planted a very interesting plant, which is a triangle directed "upwards". This plant has an amusing feature. After one year a triangle plant directed "upwards" divides into four triangle plants: three of them will point "upwards" and one will point "downwards". After another year, each triangle plant divides into four triangle plants: three of them will be directed in the same direction as the parent plant, and one of them will be directed in the opposite direction. Then each year the process repeats. The figure below illustrates this process.

Codeforces Round #118 (Div. 2) C. Plant (找规律)_第1张图片

Help the dwarfs find out how many triangle plants that point "upwards" will be in n years.

Input

The first line contains a single integer n (0 ≤ n ≤ 1018) — the number of full years when the plant grew.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cincout streams or the %I64dspecifier.

Output

Print a single integer — the remainder of dividing the number of plants that will point "upwards" in n years by 1000000007 (109 + 7).

Examples
input
1
output
3
input
2
output
10
Note

The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.





你可能感兴趣的:(找规律,水题)