【CF185A】Plant

题目描述
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 n years.

输入输出格式
输入格式:
The first line contains a single integer n n (0<=n<=10^{18}) (0<=n<=10
18
) — 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 cin, cout streams or the %I64d specifier.

输出格式:
Print a single integer — the remainder of dividing the number of plants that will point “upwards” in n n years by 1000000007 1000000007 (10^{9}+7) (10
9
+7) .

输入输出样例
输入样例#1:
1
输出样例#1:
3
输入样例#2:
2
输出样例#2:
10
说明
The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.

题目大意:如图所示,第一天有3个朝上的三角形,第二天有10个朝上的三角形,以此类推,第n天有多少个三角形?
分析:打表找规律,很显然,可以推出一个O(n)的算法:设a【i】表示第i天朝上的三角形,b【i】表示第i天朝下的三角形,容易得出:

a【i】=3a【i-1】+b【i-1】
b【i】=3
b【i-1】+a【i-1】
然后写一个暴力程序,打出n==10的表:

#include<bits/stdc++.h>
using namespace std;
int a[20],b[20];
int main()
{
    a[0]=1;
	for(int i=1;i<=10;i++)
	{
	    a[i]=3*a[i-1]+b[i-1];
	    b[i]=3*b[i-1]+a[i-1];
	    printf("%d %d\n",a[i],b[i]);
	}
    return 0;
}

运行结果如下(左侧是a【】):

3 1
10 6
36 28
136 120
528 496
2080 2016
8256 8128
32896 32640
131328 130816
524800 523776

可以找到,a【i】=4*a【i-1】-2^(i-1),但这仍然是一个递推式子,我们考虑化简。

a【i】
=4a【i-1】-2^(i-1)
= 4
(4*a【i-2】-2(i-2))-2(i-1)
=42*a【i-2】-3*2(i-1)
=…

继续推,可得(a【0】==1)

a【i】=4^i-m*2^(i-1)

这里的m是一个常数,根据规律可得出,m=2^i-1
综上所述:

a【i】=4i-(2i-1)*2^(i-1)

快速幂跑一下就有了,代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll p=1000000007;
ll n,ans1,ans2;
ll quickpow(ll a,ll b,ll c)
{
    ll ans=1;
    a%=c;
    while(b)
    {
        if(b&1) ans=(ans*a)%c;
        b>>=1;
        a=a*a%p;
    }
    return ans;
}
int main()
{
	scanf("%lld",&n);
	if(n==0)
	{
	    printf("1");
	    return 0;
	}
	ans1=quickpow(4,n,p);
	ans2=((quickpow(2,n,p)-1+p)%p*(quickpow(2,n-1,p)))%p;
	printf("%lld",(ans1%p-ans2%p+p)%p);
    return 0;
}

谢谢各位

猜你喜欢

转载自blog.csdn.net/dingxingdi/article/details/81018822
cf