博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
洛谷P4593 [TJOI2018]教科书般的亵渎(拉格朗日插值)
阅读量:5115 次
发布时间:2019-06-13

本文共 4400 字,大约阅读时间需要 14 分钟。

题意

Sol

打出暴力不难发现时间复杂度的瓶颈在于求\(\sum_{i = 1}^n i^k\)

老祖宗告诉我们,这东西是个\(k\)次多项式,插一插就行了

上面的是\(O(Tk^2)\)

下面是\(O(Tk^3)\)

// luogu-judger-enable-o2#include
#define LL long long using namespace std;const int MAXN = 66, mod = 1e9 + 7;inline LL read() { char c = getchar(); LL x = 0, f = 1; while(c < '0' || c > '9') {if(c == '0') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f;}LL N, M, a[MAXN], tot, inv[3601];LL add(LL x, LL y) { if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}void add2(LL &x, LL y) { if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}LL mul(LL x, LL y) { return 1ll * x * y % mod;}LL fp(LL a, LL p) { LL base = 1; while(p) { if(p & 1) base = mul(base, a); a = mul(a, a); p >>= 1; } return base;}LL x[MAXN], y[MAXN], fac[MAXN], ifac[MAXN], pre[MAXN], suf[MAXN];LL get(LL N, LL M) {// \sum_{i=1}^n i^m //printf("%d %d\n", N, M); LL Lim = M + 1, ans = 0; memset(y, 0, sizeof(y)); for(int i = 1; i <= Lim; i++) add2(y[i], add(y[i - 1], fp(i, M))); pre[0] = N; suf[Lim + 1] = 1; for(int i = 1; i <= Lim; i++) pre[i] = mul(pre[i - 1], add(N, -i)); for(int i = Lim; i >= 1; i--) suf[i] = mul(suf[i + 1], add(N, -i)); for(int i = 0; i <= Lim; i++) { LL up = mul(y[i], mul(pre[i - 1], suf[i + 1])), down = mul(ifac[i], ifac[Lim - i]); if((Lim - i) & 1) down = mod - down; //printf("%d %d\n", up, down); ans = add(ans, mul(up, down)); } return ans;}void solve() { N = read(); M = read(); memset(a, 0, sizeof(a)); LL ans = 0; for(LL i = 1; i <= M; i++) a[i] = read(); a[++M] = ++N; sort(a + 1, a + M + 1); for(LL i = 1; i <= M; i++) { for(LL j = i; j <= M; j++) ans = add(ans, add(get(a[j] - 1, M ), -get(a[j - 1], M))); for(LL j = i + 1; j <= M; j++) a[j] = add(a[j], -a[i]); a[i] = 0; } printf("%lld\n", ans);} int main() { inv[1] = 1; for(int i = 2; i <= 3600; i++) inv[i] = mul((mod - mod / i), inv[mod % i]); fac[0] = 1; for(int i = 1; i <= 60; i++) fac[i] = mul(i, fac[i - 1]); ifac[60] = fp(fac[60], mod - 2); for(int i = 60; i >= 1; i--) ifac[i - 1] = mul(ifac[i], i); //cout << get(10, 2) << endl; for(LL T = read();T--; solve()); return 0;}/*11044536146 2883276404640705454*/
// luogu-judger-enable-o2#include
using namespace std;const int MAXN = 103, mod = 1e9 + 7;inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f;}int add(int x, int y) { if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}int mul(int x, int y) { return 1ll * x * y % mod;}int fp(int a, int p) { int base = 1; while(p) { if(p & 1) base = mul(base, a); a = mul(a, a); p >>= 1; } return base;}int N, M, K, f[MAXN][MAXN], C[MAXN][MAXN], U[MAXN], R[MAXN], g[MAXN];int get(int U, int R) { memset(g, 0, sizeof(g)); for(int i = 1; i <= MAXN - 1; i++) for(int k = 1; k <= i; k++) g[i] = add(g[i], mul(fp(k, N - R), fp(i - k, R - 1))); int ans = 0; for(int i = 1; i <= MAXN - 1; i++) { int up = 1, down = 1; for(int j = 1; j <= MAXN - 1; j++) { if(i == j) continue; up = mul(up, add(U, -j)); down = mul(down, add(i, -j)); } ans = add(ans, mul(g[i], mul(up, fp(down, mod - 2)))); } return ans;}int main() { //freopen("a.in", "r", stdin); N = read(); M = read(); K = read(); for(int i = 0; i <= N; i++) { C[i][0] = C[i][i] = 1; for(int j = 1; j < i; j++) C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]); } for(int i = 1; i <= M; i++) U[i] = read(); for(int i = 1; i <= M; i++) R[i] = read(); f[0][N - 1] = 1; for(int i = 1; i <= M; i++) { int t = get(U[i], R[i]); for(int j = K; j <= N; j++) { for(int k = j; k <= N - 1; k++) if(k - j <= R[i] - 1) f[i][j] = add(f[i][j], mul(mul(f[i - 1][k], C[k][k - j]), C[N - 1 - k][R[i] - 1 - (k - j)])); f[i][j] = mul(f[i][j], t); } } printf("%d", f[M][K]); return 0;}/*100 3 50500 500 45613 46 45*/

转载于:https://www.cnblogs.com/zwfymqz/p/10056635.html

你可能感兴趣的文章
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
cassandra vs mongo (1)存储引擎
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>
遍历Map对象
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
#Leetcode# 209. Minimum Size Subarray Sum
查看>>
SDN第四次作业
查看>>
DM8168 DVRRDK软件框架研究
查看>>
django迁移数据库错误
查看>>
yii 跳转页面
查看>>
洛谷 1449——后缀表达式(线性数据结构)
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>
Dirichlet分布深入理解
查看>>
(转)Android之发送短信的两种方式
查看>>
字符串处理
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
ad logon hour
查看>>