分析

之前一直不知道拉格朗日插值是干什么用的,只会做模板题,做了这道题才明白这个神奇算法的用法。

由题意可知,\(f(x)\)是关于\(x\)的\(k+1\)次函数,\(g(x)\)是关于\(x\)的\(k+2\)次函数,\(ans(x)\)是关于\(x\)的\(k+3\)次函数。

由于点值连续,插值可以做到\(O(n)\),求\(g(x)\)和\(ans(x)\)都需要插值,因此时间复杂度为\(O(Tn^2 \log n)。(\)\log$是快速幂的,貌似可以通过预处理逆元优化掉,不过AC这道题已经绰绰有余了。)

代码

#include <bits/stdc++.h>
#define rin(i,a,b) for(register int i=(a);i<=(b);++i)
#define irin(i,a,b) for(register int i=(a);i>=(b);--i)
#define trav(i,a) for(register int i=head[a];i;i=e[i].nxt)
typedef long long LL;
using std::cin;
using std::cout;
using std::endl; inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
return x*f;
} const LL MOD=1234567891;
const int MAXN=130; int k;
LL s,n,d,inv[MAXN],ff[MAXN],gg[MAXN],hh[MAXN]; inline LL qpow(LL x,LL y){
LL ret=1,tt=x%MOD;
while(y){
if(y&1) ret=ret*tt%MOD;
tt=tt*tt%MOD;
y>>=1;
}
return ret;
} LL g(LL x){
if(x<=k+3) return gg[x];
LL son=1,mot=1;
rin(i,2,k+3) son=son*(x-i+MOD)%MOD;
rin(i,2,k+3) mot=mot*(1-i+MOD)%MOD;
LL ret=0;
rin(i,1,k+3){
ret=(ret+gg[i]*son%MOD*qpow(mot,MOD-2))%MOD;
son=son*qpow(x-(i+1)+MOD,MOD-2)%MOD*(x-i+MOD)%MOD;
mot=mot*qpow(k+3-i+MOD,MOD-2)%MOD*(MOD-i)%MOD;
}
return ret;
} LL h(LL x){
if(x<=k+4) return hh[x];
LL son=1,mot=1;
rin(i,2,k+4) son=son*(x-i+MOD)%MOD;
rin(i,2,k+4) mot=mot*(1-i+MOD)%MOD;
LL ret=0;
rin(i,1,k+4){
ret=(ret+hh[i]*son%MOD*qpow(mot,MOD-2))%MOD;
son=son*qpow(x-(i+1)+MOD,MOD-2)%MOD*(x-i+MOD)%MOD;
mot=mot*qpow(k+4-i+MOD,MOD-2)%MOD*(MOD-i)%MOD;
}
return ret;
} int main(){
int T=read();
while(T--){
k=read(),s=read(),n=read(),d=read();
ff[0]=0;
rin(i,1,k+3) ff[i]=(ff[i-1]+qpow(i,k))%MOD;
gg[0]=0;
rin(i,1,k+3) gg[i]=(gg[i-1]+ff[i])%MOD;
hh[0]=g(s);
rin(i,1,k+4) hh[i]=(hh[i-1]+g((s+i*d)%MOD))%MOD;
printf("%lld\n",h(n));
}
return 0;
} /*
5
120 102497463 92989700 20360484
66 105420730 97423975 32388530
95 64109604 78460286 106343540
101 66688000 92566071 49084899
102 120568505 7166048 11911911 948519230
179937457
690200633
382076592
500116309
*/

[BZOJ3453]tyvj 1858 XLkxc:拉格朗日插值的更多相关文章

  1. BZOJ3453: tyvj 1858 XLkxc(拉格朗日插值)

    题意 题目链接 Sol 把式子拆开,就是求这个东西 \[\sum_{i = 0} ^n \sum_{j = 1}^{a + id} \sum_{x =1}^j x^k \pmod P\] 那么设\(f ...

  2. BZOJ.3453.tyvj 1858 XLkxc(拉格朗日插值)

    BZOJ 题意即求\[\sum_{i=0}^n\sum_{j=1}^{a+id}\sum_{x=1}^jx^k\] 我们知道最后一个\(\sum\)是自然数幂和,设\(f(n)=\sum_{x=1}^ ...

  3. 【BZOJ】3453: tyvj 1858 XLkxc 拉格朗日插值(自然数幂和)

    [题意]给定k<=123,a,n,d<=10^9,求: $$f(n)=\sum_{i=0}^{n}\sum_{j=1}^{a+id}\sum_{x=1}^{j}x^k$$ [算法]拉格朗日 ...

  4. bzoj3453: tyvj 1858 XLkxc(拉格朗日插值)

    传送门 \(f(n)=\sum_{i=1}^ni^k\),这是自然数幂次和,是一个以\(n\)为自变量的\(k+1\)次多项式 \(g(n)=\sum_{i=1}^nf(i)\),因为这东西差分之后是 ...

  5. BZOJ 3453 - tyvj 1858 XLkxc(插值+推式子)

    题面传送门 首先根据我们刚学插值时学的理论知识,\(f(i)\) 是关于 \(i\) 的 \(k+1\) 次多项式.而 \(g(x)\) 是 \(f(x)\) 的前缀和,根据有限微积分那一套理论,\( ...

  6. 拉格朗日插值&&快速插值

    拉格朗日插值 插值真惨 众所周知$k+1$个点可以确定一个$k$次多项式,那么插值就是通过点值还原多项式的过程. 设给出的$k+1$个点分别是$(x_0,y_0),(x_1,y_1),...,(x_k ...

  7. Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值

    The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...

  8. 常系数齐次线性递推 & 拉格朗日插值

    常系数齐次线性递推 具体记在笔记本上了,以后可能补照片,这里稍微写一下,主要贴代码. 概述 形式: \[ h_n = a_1 h_{n-1}+a_2h_{n-2}+...+a_kh_{n-k} \] ...

  9. 快速排序 and 拉格朗日插值查找

    private static void QuictSort(int[] zu, int left, int right) { if (left < right) { ; ; ]; while ( ...

随机推荐

  1. [转帖]Spring Cloud底层原理

    拜托!面试不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA 毫无疑问,Spring Cloud 是目前微服 ...

  2. seata demo

    0. 介绍 2019 年 1 月,阿里巴巴中间件团队发起了开源项目 Fescar(Fast & EaSy Commit And Rollback),和社区一起共建开源分布式事务解决方案.Fes ...

  3. MySQL5.6解压版服务无法启动—系统错误1067

    记录一下今天被坑了一下午的BUG 就从半个月前说起吧 当时....................................................................... ...

  4. SSM笔记

    Spring Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象.也可以称之为项目中的粘合剂. Spring的核心思想是IoC(控制 ...

  5. 三校联训 小澳的葫芦(calabash) 题解

    题面:小澳的葫芦[ 题目描述]小澳最喜欢的歌曲就是<葫芦娃>.一日表演唱歌,他尽了洪荒之力,唱响心中圣歌.随之,小澳进入了葫芦世界.葫芦世界有 n 个葫芦,标号为 1~ n. n 个葫芦由 ...

  6. [CF750G] New Year and Binary Tree Paths

    目录 简单的 组合的 题目链接 简单的 设从节点\(x\)开始不断往左儿子走h-1步,则编号和为\(x\sum_{i=0}^{h-1}2^i=x(2^h-1)\). 若倒数第\(i\)步走向的是右儿子 ...

  7. Centos7 升级php版本到php7

    一.首先查看是否有老版本 yum list installed | grep php 二.如果安装的有 yum remove php.x86_64 php-cli.x86_64 php-common. ...

  8. echarts图标使用(一)

    var data = []; // Parametric curve // for (var t = 0; t < 25; t += 0.001) { // var x = (1 + 0.25 ...

  9. nginx正常启动,ip直接访问失败问题

    1.查看iptables服务示范启动 service iptables status 2.没有安装的话安装 yum install iptables-services 3.修改配置文件, 添加一行: ...

  10. echarts 给legend图例加个标题式文字设置为普通文本不可点击

    legend: [ { orient: "horizontal", // 'vertical' x: "68%", // 'center' | 'left' | ...