2018icpc南京 Problem G. Pyramid

题目
题意
求n行三角形中等边三角形个数,三角形的顶点要求是小三角形的顶点,图中的三角形也算。
Input
The first line contains an integer t (1 ≤ t ≤ 106) — the number of test cases.
Each of the next t lines contains an integer n (1 ≤ n ≤ 109) — the degree of the pattern.
Output
For each test case, print an integer in one line — the number of regular triangles modulo 1e9 + 7.
Example
standard input
3
1
2
3
standard output
1
5
15
在这里插入图片描述

思路
看到图中给的特殊三角形,想到底为4时存不存在这样类似的三角形,画了图,很明显存在。
记底为3的三角形正常的三角形记为t1个,那么结果要多加t1 * 2个,其他情况类似。
我们可以推出一个伪公式
例如 t5 = t1 + t2 + 3 * t3 + 3 * t4 + 3 * t5
随着底边增大,子三角形个数也会变,虽然能推出变化公式或者打表出结果,我还是在训练赛过程中手算了好几项。
发现 1,5,15,35,70,126,210
根据高中数列找规律的想法,我们将其两两相减,再两两相减,知道数列有规律为止,最后发现是四阶差分数列,设通项为an^4 + bn^4 + cn^3 + dn^2 + en + f
得到 n^4/24 + n^3/4 + 11*n^2/24 + n/4
虽然可以化简,但是无所谓了。

对了,g++11可以过,g++17 tle…

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 1e9+7;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = ans * base % mod;
            base = base * base % mod;
            b >>= 1;
        }
        return ans % mod;
    }
}
using namespace std;
using namespace fastIO;
const int N = 1e6+5;
int Case,n;
int a[N],vis[N];
ll cal(int n){
	ll ans=1ll*qpow(24,mod-2)*qpow(n,4)+1ll*qpow(4,mod-2)*qpow(n,3)+11ll*qpow(24,mod-2)*qpow(n,2)+1ll*qpow(4,mod-2)*n;
	return ans%mod;
}
int main(){
	input(Case);
	while(Case--){
		input(n);
		printf("%lld\n",cal(n));
	} 
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值