[Brush title] Luogu P2675 "Qu Pa's Number Game" T3-Triangle Holy Land

topic background

King 1 takes everyone to the heart of the digital realm: Triangle Sanctuary.

Topic description

It's not that the triangle is the most stable figure, the center of the digital kingdom is composed of an inverted triangle. There is a row of numbers at the top of this inverted triangle, from 1 to N. 1 ~ N can swap positions. The numbers in each row after that are obtained by adding the two adjacent numbers in the previous row. In this way, the bottom is a relatively large number! Digital Domain calls this number the "base". King 1 hopes that the bigger the "base", the better, but it is too cumbersome to do the addition every time. He hopes that you can help him calculate the maximum value of this number through programming. But this value can be very large, so please output the result of mod 10007.

Task: Given N, find the maximum value of the basis of the triangle 1~N and then go to mod 10007.

Input and output format

Input format:

an integer N

Output format:

An integer representing the largest "base" of the triangle formed by 1~N

Input and output example

Input example #1:

4

Sample output #1:

24

Input example #2:

1125

Sample output #2:

700

illustrate

data:

20% 0<=N<=100

50% 0<=N<=3000

100% 0<=N<=1000000

Example explanation:

1 3 4 2

4 7 6

11 13

24 is the maximum value when N=4, and of course there are other forms.

PS: It is called Triangle Holy Land, in fact, it is a triangle~

The data for this question has been updated, and it is now all correct!

Don't program for data!

answer

After drawing the triangle, it is found that the number of times a number in the sequence is added to the "base" is the number of solutions from its position to the bottom.
Then, \(ans=C_{n-1}^0a_0+C_{n-1 }^1a_1+C_{n-1}^2a_2+...+C_{n-1}^{n-1}a_{n-1}=\sum_{i=0}^{n-1}C_{ n-1}^ia_i\)
So the more you go to the middle, the more times you are added, so greedily take the sequence, and then \(O(n)\) just use Lucas to calculate

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=1000000+10,Mod=1e4+7;
int n,A[MAXN];
ll res,fac[MAXN],inv[MAXN];
template<typename T> inline void read(T &x)
{
    T data=0,w=1;
    char ch=0;
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')w=-1,ch=getchar();
    while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
    if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline ll qexp(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1)res=res*a%Mod;
        a=a*a%Mod;
        b>>=1;
    }
    return res;
}
inline void init()
{
    fac[0]=1;
    for(register int i=1;i<Mod;++i)fac[i]=fac[i-1]*i%Mod;
    inv[Mod-1]=qexp(fac[Mod-1],Mod-2);
    for(register int i=Mod-2;i>=0;--i)inv[i]=inv[i+1]*(i+1)%Mod;
}
inline ll C(ll n,ll m)
{
    if(n<m)return 0;
    if(n<=Mod&&m<=Mod)return fac[n]*inv[m]%Mod*inv[n-m]%Mod;
    else return C(n/Mod,m/Mod)*C(n%Mod,m%Mod)%Mod;
}
int main()
{
    read(n);
    init();
    if(n&1)
    {
        A[(n>>1)+1]=n;A[(n>>1)+2]=n-1;
        for(register int i=(n>>1);i>=1;--i)A[i]=A[i+1]-2;
        for(register int i=(n>>1)+3;i<=n;++i)A[i]=A[i-1]-2;
    }
    else
    {
        A[1]=1;
        for(register int i=2;i<=(n>>1);++i)A[i]=A[i-1]+2;
        A[(n>>1)+1]=n;
        for(register int i=(n>>1)+2;i<=n;++i)A[i]=A[i-1]-2;
    }
    for(register int i=1;i<=n;++i)(res+=C(n-1,i-1)*A[i]%Mod)%=Mod;
    write(res,'\n');
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325100255&siteId=291194637