日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

BZOJ2655 Calc - dp 拉格朗日插值法

發布時間:2023/10/11 103 老码农
生活随笔 收集整理的這篇文章主要介紹了 BZOJ2655 Calc - dp 拉格朗日插值法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

BZOJ2655 Calc

參考

題意:

  給定n,m,mod,問在對mod取模的背景下,從【1,m】中選出n個數相乘可以得到的總和為多少。

思路:

  首先可以發現dp方程 ,假定dp【m】【n】表示從【1 ~ m】中選出n個數乘積的和,

那么dp【m】【n】 = dp【m-1】【n】 + dp【m-1】【n-1】*m*n。

但是這道題的m有1e9那么大,不能dp完,不過我們可以發現,dp【x】【n】 是關于x的2*n多項式,

所以,我們只要先求出0~2*n的dp值,再用拉格朗日插值法算出dp【m】【n】的即可。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <cctype>
#include <queue>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int ,pii> p3;
//priority_queue<int> q;//這是一個大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用來壓行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFFLL; //
const ll nmos = 0x80000000LL; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; // const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/ const int maxn = ;
ll dp[maxn][maxn],x[maxn],y[maxn];
int m,n,mod; ll ksm (ll a,ll b){
ll res = ;
while(b>){
if(b&) res = (res * a)%mod;
a = (a * a)%mod;
b >>= ;
}
return res;
}
ll lagerange(int k){
ll res = ;
for(int i=; i<=*n; i++){
ll s1=,s2 = ; for(int j=; j<=*n; j++){
if(i==j)continue;
s1 = 1ll*(s1 * (k - x[j] + mod)%mod)%mod;
s2 = 1ll*(s2 * ((x[i] - x[j] + mod)%mod))%mod;
}
res = (res + 1ll*s1 * ksm(s2,mod-) % mod * y[i] % mod+mod)%mod;
}
return res;
}
int main(){ scanf("%d%d%d", &m, &n, &mod);
dp[][] = ;
for(int i=; i<=*n; i++){
dp[i][] = ;
for(int j=; j<=n; j++){
dp[i][j] = 1ll*dp[i-][j-] * i % mod * j + dp[i-][j];
dp[i][j] = dp[i][j]%mod;
}
} if(m <= * n){
printf("%lld\n", dp[m][n]);
return ;
} for(int i=; i<=*n; i++) x[i] = i,y[i] = dp[i][n]; printf("%lld\n",lagerange(m)); return ;
}

BZOJ2655

總結

以上是生活随笔為你收集整理的BZOJ2655 Calc - dp 拉格朗日插值法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。