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

最新文章

  1. OpenGL 像素在内存中的排列方式
  2. [Linux][Hadoop] 将hadoop跑起来
  3. 第四章:javascript: 栈
  4. dedeCMS安装,前端样式不显示
  5. Strlen()与sizeof()
  6. 使用NodeJS+AngularJS+MongoDB实现一个Web数据扒取-分析-展示的系统
  7. 省市选择(基于zepto.js)
  8. SignTool.exe(签名工具)
  9. 华为u8800怎样root?
  10. MVC-05 Model(2)
  11. C#获取客户端IP地址
  12. 队列ADT
  13. python3练习-发送IP地址到邮箱
  14. websocket 工作原理
  15. JavaScript错误:Maximum call stack size exceeded错误
  16. 上网爱快?EasyRadius FOR 爱快V2接口测试版正式推出,欢迎广大爱迷们测试噢
  17. js delete可以删除对象属性及变量
  18. java中Cookie使用问题(message:invalid character [32] was present in the Cookie value)
  19. IIS7.5 配置 PHP 5.3.5
  20. [转帖]Cocos2d-x 3.0rc0 的Win32工程添加CocoStudio库

热门文章

  1. 【iOS】Ineligible Devices || “无法下载应用程序”
  2. Java 性能优化(一)
  3. selenium操作cookies实现免密登录,自动发微博
  4. LeetCode :2.两数相加 解题报告及算法优化思路
  5. caddy &amp; grpc(3) 为 caddy 添加一个 反向代理插件
  6. Python递归函数,二分查找算法
  7. POI通用导出Excel数据(包括样式设计)
  8. JS鼠标吸粉特效
  9. 高速开车换底盘记:Windows 与 Linux 部署都抗住了,但修车任务艰巨
  10. JMeter使用JSON Extractor插件实现将一个接口的JSON返回值作为下一个接口的入参