生活随笔
收集整理的這篇文章主要介紹了
P4781 【模板】拉格朗日插值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
把公式實現一下即可:
當xxx連續的時候可以優化為O(N)O(N)O(N)。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std
;
typedef long long LL
;
typedef unsigned long long ULL
;
typedef pair
<int,int> PII
;const int N
=1000010,mod
=998244353,INF
=0x3f3f3f3f;
const double eps
=1e-6;LL n
,k
;
pair
<LL
,LL
> p
[N
];LL
qmi(LL a
,LL b
) {LL ans
=1;a
%=mod
; a
+=mod
; a
%=mod
;while(b
) {if(b
&1) ans
=ans
*a
%mod
;a
=a
*a
%mod
;b
>>=1;}return ans
%mod
;
}int main()
{
scanf("%lld%lld",&n
,&k
);for(int i
=1;i
<=n
;i
++) scanf("%lld%lld",&p
[i
].X
,&p
[i
].Y
);LL ans
=0;for(int i
=1;i
<=n
;i
++) {LL now1
=p
[i
].Y
,now2
=1;for(int j
=1;j
<=n
;j
++) {if(i
==j
) continue;now1
*=((k
-p
[j
].X
+mod
)%mod
); now1
%=mod
;now2
*=p
[i
].X
-p
[j
].X
; now2
%=mod
;}now2
=qmi(now2
,mod
-2);ans
+=now1
*now2
%mod
; ans
%=mod
;}printf("%lld\n",ans
%mod
);return 0;
}
總結
以上是生活随笔為你收集整理的P4781 【模板】拉格朗日插值的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。