【CF185A】Plant

本文介绍了一种特殊的三角形植物模型,该植物每年会分裂出新的植物,形成一个有趣且复杂的增长模式。文章通过数学分析找到了植物数量增长的规律,并提供了一个高效的计算公式来预测任意年份朝上三角形植物的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
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;
}

谢谢各位

``` <?xml version="1.0" encoding="UTF-8"?> <!D0CTYPE mapper PuBLIc "-//mybatis.org//0To Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper,dtd"s <mapper namespace="cn.com.cnnp erdb.rmr.conm.psatype.mapper .PsaTypeMapper"> 通用查询映射结果--> <resultMap id="psaTpeResultMap" type="cn.com.cnnp.endb.rmr.conm.psatype. vo .PsaTypeV0"> <result column="id" property="id"/> <result column="code" property="code"/> <result column="name" property="name"/> <result column="parent_id" property="parentId"/> <result column="remark" property="remark"/> <result column="plant_id" property="plantId"/> <result column="unit_id” property="unitId"/> <result column="update_user" property="updateUser"/> <result column="update_time" property="updateTime"/> <result column="create_user" property="createUser"/> <result column="create_time" property="createTime"/> <result column="is_deleted" property="isDeleted"/> </resultMap> <select id="selectPsaTypePage" resultType="cn .com.cnnp.erdb.rmr.conm.psatype. vo.PsaTypeVo"> select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time, a.is deleted, ifnull(parent.code,")as parentCode, ifnull(parent.name,"')as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id left join erm_dm.dm_eg_unit u on a.unit_id = u.id <where> a.is_deleted = 0 <if test="vo.plantId != null and vo.plantId != "> and a.plant_id =#{vo.plantId} </if> sif test="vo,unitId != null and vo.unitId !=!"> and a.unit id= #{vo.unitId} </1f> </where> ORDER BY a.'plant_id’,(u.*unit_no*+0),parent.code, a.code </select> <select id="spaqe resultType="cn.com,cnnp.epdb.rmn.conm.psatype.vo.PsaTypeselectVo": select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time, a.is deleted, parent.code as parentCode, parent.name as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id <where> a.is_deleted= 0 <choose> <when test="parentId != null and parentId != '""> and a.parent_id = #{parentId} </when> <otherwise> and(a.parent_id is null or a.parent_id ='') </otherwise> </choose> <if test="plantId != null and plantId != "'"> and a.plant_id = #{plantId} </if> <if test="unitId != null and unitId !="'"> and a.unit_id = #{unitId} </if> </where> 0RDER BY a.plant_id,parent.code, a.code </select> <select id="spages" resultType="cn.com .cnnp.endb.rmr conm.psatype. vo .PsaTypeSelectVo"> select a.id, a.code, a.name, a.parent id, a.plant id, a.unit id, a.remark, a.update user, a.update time, a.create user, a.create time. a is deleted parent.code as parentCode, parent.name as parentName from rmr_cf_psa_type a left join rmr_cf_psa_type parent on a.parent_id = parent.id cwhere> a.is_deleted= 0 <choose> swhen test="parentId != null and parentId != '!"> and a.parent_id = #{parentId} <when test="parentId != null and parentId != ''"> and a.parent_id = #{parentId} </when> <otherwise> AND a.1d NOT IN (SELEcT parent_id FRoM rmr_cf_psa_type WHERE is_deleted = '@' AND parent_1d IS NOT NULL) </otherwise> </choose> <if test="plantId != null and plantId !=''"> and a.plant_id =#{plantId} </if> <if test="unitId != null and unitId != '·“> and a.unit_id = #{unitId} </if> </where> ORDER BY a.`plant_id`,parent.code, a.code </select> <select id="stdDeviceType" resultType="cn.com.gnnp.ardb.rmr.gon.psatype.vo.stdDeviceTypelistVo"> select id, name , code, fid, level from erdb_device_type <where> is_deleted =0 <choose> <when test="level != null and level != '"> <choose> <when test="level =='',toString()"> and fid in(select id from erdb_device_type where fid = #{fid}) and level = 3 </when> <otherwise> and level = #{level} <if test="fid != null and fid != ""> and fid = #{fid} </if> </otherwise> </choose> </when> <otherwise> and level = 3 <if test="fid != null and fid != ''"> and fid = #{fid} </if> </otherwise> </choose> </where> </select> /mapper>```代码所实现的功能详情解析
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值