Codeforces Round #118 (Div. 1) A. 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 inn 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 usecin, cout streams or the%I64d specifier.

Output

Print a single integer — the remainder of dividing the number of plants that will point "upwards" inn 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.

简单明了的题,求第n的图的向上的三角形是多少个。水题。我看了看都是那矩阵快速幂过的。但是我觉得其实快速幂就够了。

思路,我是注意到对于一个图形,假设它的边长是k,那么注意到,当不看最底层的k个向上的三角形的时候,你会发现每一个的向上三角形都和一个向下的三角形对应起来了!那么向上的三角形的个数=(总个数-k)/2+k!

而总共三角形的个数=4^i(每次多三个相同的大三角形)

一边的长度=2^i

所以公式,对于每一个输入n,向上三角形个数=(4^n-2^n)/2+2^n;

我当时就直接快速幂了。但是一上来就wa4。比较年轻的错。

最大的wa点:对取模运算的认识

首先,减法取模(a-b)%mod是不对的,应当转换为(a-b+mod)%mod,防止负数出现(wa27)

然后,除法取模(a/b)%mod也是不对的,这个转换比较复杂,我在这里看的。总而言之,对于本体,mod=1e9+7,b=2

故转换成(a*(5*1e8+4))%mod(ps:这个其实是不能运算的,因为浮点数不能被整除,这里是为了看和写的方便)。(wa4)

代码

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<stack>
#include<vector> 
#include<set>
#include<math.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=1e6+5;
const ll inf=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;

//(4^i-2^i)/2+2^i//
int flag=0;
ll que_pow(ll x,ll n){
	ll ans=1;
	while(n>0){
		if(n%2==1){
			ans=(ans*x)%mod;
		}
		x=(x*x)%mod;
		n=n/2;
	}
	return ans;
}
//用除法取模// 
int main(){
	ll n;
	cin>>n;
//	while(cin>>n/*n++<100*/){
		ll y=que_pow(2,n);
		ll x=que_pow(4,n);
//		cout<<x<<" "<<y<<endl;
		ll ans=(((x-y+mod)%mod)*(500000000+4))%mod+y%mod;
		ans=ans%mod; 
		cout<<ans<<endl;
//	}
}


解决了这2个问题,恭喜你,ac(虽然是水题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值