我是靠谱客的博主 魔幻音响,最近开发中收集的这篇文章主要介绍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<bits/stdc++.h>
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("%I64dn", (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.

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.





最后

以上就是魔幻音响为你收集整理的Codeforces Round #118 (Div. 2) C. Plant (找规律)的全部内容,希望文章能够帮你解决Codeforces Round #118 (Div. 2) C. Plant (找规律)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部