And wrong of rehabilitation training -bzoj5339: loj2578: p4593: [TJOI2018] textbook blasphemy

Subject to the effect

Topic Link

answer

First \ (a \) sort.
\ (K \) looks strange blood equal to the number of consecutive segments, it is to be noted that when there \ (a_i + 1 = a_ { i + 1} \) , the period between successive although they are empty, but but also count; when \ (a_m = n \) , the last period of continuous segments do not count.
Consider the process of the game: to set the current maximum blood \ (P \) , the first shot is \ (Q \) Zhang profane, the resulting score is: \ (\ SUM \ limits_. 1} {I = PI ^ ^ K - \ SUM \ limits_ I = {Q} ^ {m} ({a_i-Q-A_. 1}) ^ K \) .
After the part can be determined directly.
The first part \ (\ SUM \ limits_. 1} {I = PI ^ ^ K \) , by observing theView solution to a problemIt is found that formula is a demand on \ (P \) a \ (k + 1 \) degree polynomial, can be \ (p = 1,2, ..., k + 2 \) is substituted into solving violent afford \ (k + 2 \) a on the curve of the polynomial point, and then by seeking the Lagrange interpolation polynomial.

Code
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define view(u,k) for(int k=fir[u];~k;k=nxt[k])
#define LL long long
#define maxn 57
using namespace std;
LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(!isdigit(ch)&&ch!='-')ch=getchar();
    if(ch=='-')f=-1,ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    return x*f;
}
void write(int x)
{
    if(x==0){putchar('0'),putchar('\n');return;}
    int f=0;char ch[20];
    if(x<0)putchar('-'),x=-x;
    while(x)ch[++f]=x%10+'0',x/=10;
    while(f)putchar(ch[f--]);
    putchar('\n');
    return;
}
const LL mod=1e9+7;
LL n,a[maxn];
int t,m,f[maxn],b[maxn],qy[maxn],ans,sz,k;
int mul(int x,int y){int res=1;while(y){if(y&1)res=(LL)res*x%mod;x=(LL)x*x%mod,y>>=1;}return res;}
int mo(int x){return x>=mod?x-mod:x;}
void prew()
{
    rep(i,1,sz)qy[i]=mo(qy[i-1]+mul(i,k)),f[i]=0;
    f[0]=1;
    rep(i,1,sz)dwn(j,i,1)f[j]=mo(f[j]+(LL)f[j-1]*(mod-i)%mod);
    reverse(f,f+sz+1);
    rep(i,1,sz)
    {
        int lst=0,num=1,nyx=mul(mod-i,mod-2);
        rep(j,1,sz)if(i!=j)num=(LL)num*mo(i-j+mod)%mod;
        num=(LL)mul(num,mod-2)*qy[i]%mod;
        rep(i,0,sz-1)
        {
            lst=(LL)mo(f[i]-lst+mod)*nyx%mod,b[i]=mo(b[i]+(LL)lst*num%mod);
        }
    }
}
int getf(LL x)
{
    if(x<=0)return 0;
    x%=mod;
    int res=0,now=1;
    rep(i,0,sz-1)res=mo(res+(LL)b[i]*now%mod),now=(LL)now*x%mod;
    return res;
}
int main()
{
    t=read();
    while(t--)
    {
        n=read(),m=read(),ans=0;k=m+1;
        rep(i,1,m)a[i]=read();
        sort(a+1,a+m+1);
        if(a[m]==n)k--;
        else a[++m]=n+1;sz=k+2;
        prew();
        rep(i,1,m)
        {
            ans=mo(ans+getf(a[m]-a[i-1]-1));
            rep(j,i,m-1)ans=mo(ans-mul(a[j]-a[i-1],k)+mod);
        }
        write(ans);
        rep(i,0,sz)b[i]=0;
    }
    return 0;
}
Some thoughts

When it comes to seeking natural numbers power and, have to say that some of the provincial election day1t3 ......
think about it, for some hand very healthy people, may write Lagrange fast write positive solution than violence?

Guess you like

Origin www.cnblogs.com/xzyf/p/11593830.html