Gym 101981G - Pyramid - [打表找規律][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem G]

題目連接:http://codeforces.com/gym/101981/attachmentsc++

The use of the triangle in the New Age practices seems to be very important as it represents the unholy
trinity (Satan, the Antichrist and the False Prophet bringing mankind to the New World Order with
false/distorted beliefs). The triangle is of primary importance in all Illuminati realms, whether in the
ritual ceremonies of the Rosicrucians and Masons or the witchcraft, astrological and black magic practices
of other Illuminati followers.
One day you found a class of mysterious patterns. The patterns can be classified into different degrees. A
pattern of degree n consists of n*(n+1)/2 small regular triangles with side length 1, all in the same direction,
forming a big triangle. The figure below shows the pattern of degree 3. All small regular triangles are
highlighted.ide

Since the pattern contains many regular triangles, which is very evil and unacceptable, you want to
calculate the number of regular triangles formed by vertices in the pattern, so that you can estimate the
strength of Illuminati. It is not necessary that each side of regular triangles is parallel to one side of the
triangles. The figure below shows two regular triangles formed by vertices in a pattern of degree 3.函數

Since the answer can be very large, you only need to calculate the number modulo 10^9 + 7.spa

Input
The first line contains an integer t (1 ≤ t ≤ 10^6) — the number of test cases.
Each of the next t lines contains an integer n (1 ≤ n ≤ 10^9) — the degree of the pattern.3d

Output
For each test case, print an integer in one line — the number of regular triangles modulo 10^9 + 7.code

Example
standard input
3
orm

1blog

2ci

3rem

standard output
1

5

15

題意:

上面那個圖形的度數爲 $3$,裏面包含了 $15$ 個正三角形。如今給出度數 $n$,讓你找出那樣一個圖形裏面包含多少個正三角形。

 

題解:

暴力打出度數在 $1 \sim 20$ 內的表,發現規律是三階差分是等差數列 $4,5,6,7,\cdots$(真的我沒有開玩笑……),或者說四階差分是常數 $1$。而後果斷推了個遞推式用矩陣快速冪交了一發……TLE+1。

而後知道了只能 $O(1)$ 地求,開始考慮推通項公式。類比於四次函數求四階導數以後爲常數,換句話說通向公式應當是一個四次多項式。

所以能夠假設 $a_n = An^4 + Bn^3 + Cn^2 + Dn + E$,而後將前五項 $a_1 = 1, a_2 = 5, a_3 = 15, a_4 = 35, a_5 = 70$ 代入解五元一次線性方程組,

解得 $A = \frac{1}{24}, B = \frac{1}{4}, C = \frac{11}{24}, D = \frac{1}{4}, E = 0$。

(注意不要忘了除法是乘逆元……已經有兩次由於這個WA+n了……)

而後實際上,OEIS搜一下就能夠知道 $a_n = C_{n+3}^{4}$。

 

AC代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll n;
ll fpow(ll a,ll n)
{
    ll res=1,base=a%mod;
    while(n)
    {
        if(n&1) res*=base, res%=mod;
        base*=base, base%=mod;
        n>>=1;
    }
    return res%mod;
}
ll inv(ll a){return fpow(a,mod-2);}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%I64d",&n);
        ll ans=0;
        ans+=fpow(n,4), ans%=mod;
        ans+=6*fpow(n,3)%mod, ans%=mod;
        ans+=11*fpow(n,2)%mod, ans%=mod;
        ans+=6*n%mod, ans%=mod;
        ans*=inv(24), ans%=mod;
        printf("%I64d\n",ans);
    }
}
相關文章
相關標籤/搜索