https://www.luogu.org/problemnew/show/P4948

这篇博客主要目的是存一下的dls的神奇板子,本来应该是推公式或者二分做的

但是dls的插值板子直接写好了这个特殊式子的算法......

#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mp make_pair
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#pragma GCC optimize("unroll-loops")
#define inline inline __attribute__( \
(always_inline, __gnu_inline__, __artificial__)) \
__attribute__((optimize("Ofast"))) __attribute__((target("sse"))) \
__attribute__((target("sse2"))) __attribute__((target("mmx")))
#define IO ios::sync_with_stdio(false);
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define for_node(x,i) for(int i=head[x];i;i=e[i].next)
#define show(x) cout<<#x<<"="<<x<<endl
#define showa(a,b) cout<<#a<<'['<<b<<"]="baidu<a[b]<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
using namespace std;
const int maxn=1e6+10,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const ll mod=1e9+7;
const double PI=acos(-1.0);
//head
int casn,n,m,k;
int num[maxn];
ll a[maxn];
ll pow_mod(ll a,ll b,ll c=mod,ll ans=1){while(b){if(b&1) ans=(a*ans)%c;a=(a*a)%c,b>>=1;}return ans;} namespace polysum {
const int maxn=101000;
const ll mod=1e9+7;
ll a[maxn],f[maxn],g[maxn],p[maxn],p1[maxn],p2[maxn],b[maxn],h[maxn][2],C[maxn];
ll calcn(int d,ll *a,ll n) {//d次多项式(a[0-d])求第n项
if (n<=d) return a[n];
p1[0]=p2[0]=1;
rep(i,0,d) {
ll t=(n-i+mod)%mod;
p1[i+1]=p1[i]*t%mod;
}
rep(i,0,d) {
ll t=(n-d+i+mod)%mod;
p2[i+1]=p2[i]*t%mod;
}
ll ans=0;
rep(i,0,d) {
ll t=g[i]*g[d-i]%mod*p1[i]%mod*p2[d-i]%mod*a[i]%mod;
if ((d-i)&1) ans=(ans-t+mod)%mod;
else ans=(ans+t)%mod;
}
return ans;
}
void init(int maxm) {//初始化预处理阶乘和逆元(取模乘法)
f[0]=f[1]=g[0]=g[1]=1;
rep(i,2,maxm+4) f[i]=f[i-1]*i%mod;
g[maxm+4]=pow_mod(f[maxm+4],mod-2);
per(i,1,maxm+3) g[i]=g[i+1]*(i+1)%mod;
}
ll polysum(ll n,ll *a,ll m) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]
// m次多项式求第n项前缀和
a[m+1]=calcn(m,a,m+1);
rep(i,1,m+1) a[i]=(a[i-1]+a[i])%mod;
return calcn(m+1,a,n-1);
}
ll qpolysum(ll R,ll n,ll *a,ll m) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]*R^i
if (R==1) return polysum(n,a,m);
a[m+1]=calcn(m,a,m+1);
ll r=pow_mod(R,mod-2),p3=0,p4=0,c,ans;
h[0][0]=0;
h[0][1]=1;
rep(i,1,m+1) {
h[i][0]=(h[i-1][0]+a[i-1])*r%mod;
h[i][1]=h[i-1][1]*r%mod;
}
rep(i,0,m+1) {
ll t=g[i]*g[m+1-i]%mod;
if (i&1) p3=((p3-h[i][0]*t)%mod+mod)%mod,p4=((p4-h[i][1]*t)%mod+mod)%mod;
else p3=(p3+h[i][0]*t)%mod,p4=(p4+h[i][1]*t)%mod;
}
c=pow_mod(p4,mod-2)*(mod-p3)%mod;
rep(i,0,m+1) h[i][0]=(h[i][0]+h[i][1]*c)%mod;
rep(i,0,m+1) C[i]=h[i][0];
ans=(calcn(m,C,n)*pow_mod(R,n)-c)%mod;
if (ans<0) ans+=mod;
return ans;
}
} int main() {
//#define test
#ifdef test
auto _start = chrono::high_resolution_clock::now();
freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif
IO;
ll n,r,k;
cin>>n>>r>>k;
polysum::init(k+5);
rep(i,0,2010) a[i]=pow_mod(i,k);
ll ans=polysum::qpolysum(r,n+1,a,k+1);
if(k==0) ans=(ans-1+mod)%mod;
cout<<ans<<endl;
#ifdef test
auto _end = chrono::high_resolution_clock::now();
cerr << "elapsed time: " << chrono::duration<double, milli>(_end - _start).count() << " ms\n";
fclose(stdin);fclose(stdout);system("out.txt");
#endif
return 0;
}

  

洛谷 P4948 拉格朗日多项式插值(杜老师板子)的更多相关文章

  1. 洛谷.3803.[模板]多项式乘法(FFT)

    题目链接:洛谷.LOJ. FFT相关:快速傅里叶变换(FFT)详解.FFT总结.从多项式乘法到快速傅里叶变换. 5.4 又看了一遍,这个也不错. 2019.3.7 叕看了一遍,推荐这个. #inclu ...

  2. 多项式求逆元详解+模板 【洛谷P4238】多项式求逆

    概述 多项式求逆元是一个非常重要的知识点,许多多项式操作都需要用到该算法,包括多项式取模,除法,开跟,求ln,求exp,快速幂.用快速傅里叶变换和倍增法可以在$O(n log n)$的时间复杂度下求出 ...

  3. 【洛谷】P1067 多项式输出

    原题链接:P1067 多项式输出 题目分析:学长推荐的OJ网站 --洛谷,发现挺好用的还可以下载提交出错的数据. 废话就不多说了,这道题属于基础题.提交出错主要是因为一些小细节不到位,这里就不一一赘述 ...

  4. 洛谷.3803.[模板]多项式乘法(NTT)

    题目链接:洛谷.LOJ. 为什么和那些差那么多啊.. 在这里记一下原根 Definition 阶 若\(a,p\)互质,且\(p>1\),我们称使\(a^n\equiv 1\ (mod\ p)\ ...

  5. 【洛谷4238】 多项式求逆(NTT,分治)

    前言 多项式求逆还是爽的一批 Solution 考虑分治求解这个问题. 直接每一次NTT一下就好了. 代码实现 #include<stdio.h> #include<stdlib.h ...

  6. 洛谷.4512.[模板]多项式除法(NTT)

    题目链接 多项式除法 & 取模 很神奇,记录一下. 只是主要部分,更详细的和其它内容看这吧. 给定一个\(n\)次多项式\(A(x)\)和\(m\)次多项式\(D(x)\),求\(deg(Q) ...

  7. 洛谷.4238.[模板]多项式求逆(NTT)

    题目链接 设多项式\(f(x)\)在模\(x^n\)下的逆元为\(g(x)\) \[f(x)g(x)\equiv 1\ (mod\ x^n)\] \[f(x)g(x)-1\equiv 0\ (mod\ ...

  8. [洛谷P4213]【模板】杜教筛(Sum)

    题目大意:给你$n$,求:$$\sum\limits_{i=1}^n\varphi(i),\sum\limits_{i=1}^n\mu(i)$$最多$10$组数据,$n\leqslant2^{31}- ...

  9. 2019.01.01洛谷 P4725/P4726 多项式对数/指数函数(牛顿迭代)

    4725传送门 4726传送门 解析 代码: #include<bits/stdc++.h> #define ri register int using namespace std; in ...

随机推荐

  1. rocketmq在linux搭建双master遇到的坑

    我的环境 两台阿里云centos7服务器 首先,去官网下载解压包,解压. 然后进入bin目录,需要修改runserver.sh文件和runbroker.sh文件.因为rocketmq默认配置文件需要的 ...

  2. ESB简介及选型(转) http://www.cnblogs.com/skyme/archive/2012/08/06/2623414.html

    什么是ESB 企业服务总线(Enterprise Service Bus,ESB)的概念是从面向服务体系架构(Service Oriented Architecture, SOA)发展而来的.SOA描 ...

  3. 极光推送API

    https://github.com/jpush/jpush-api-ruby-client/blob/master/docs/Guides.md#%E6%9B%B4%E6%96%B0%E8%AE%B ...

  4. linux 内核模块makefile通用模板

    ifneq ($(KERNELRELEASE),)# 在 mylist 后面添加需要编译的模块数量 mylist=hello.o a.o# 为每一个模块添加所需的文件 hello-objs := ma ...

  5. log4j 基础教程【转】

    参考引用自: http://javacrazyer.iteye.com/blog/1135493 我的git地址: https://git.oschina.net/KingBoBo/Log4JDemo ...

  6. JavaScript中调皮的undefined

    JavaScript中调皮的undefined 在JavaScript中undefined只是一个标识符,不是关键字,这个很不靠谱的标识符还不能像其他符号一样随意使用,一方面是需要它的原始值保持不变, ...

  7. Elasticsearch集群节点配置详解

    注意:如果是在局域网中运行elasticsearch集群也是很简单的,只要cluster.name设置一致,并且机器在同一网段下,启动的es会自动发现对方,组成集群. 2.elasticsearch- ...

  8. 【1】Java中double转BigDecimal的注意事项

    项目遇到该问题 先上结论:不要直接用double变量作为构造BigDecimal的参数. 线上有这么一段Java代码逻辑: 1,接口传来一个JSON串,里面有个数字:57.3. 2,解析JSON并把这 ...

  9. navicat and connection is being used

    1.在已经保存的连接上上编辑,测试连接成功,但是点击连接就会一直提示 connection is being used 2.需要新建一个连接,才能使用,不能再已保存的上面修改

  10. 31.【微服务架构】SpringCloud之Feign(五)

    Feign简介 Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Fei ...