(今天碰到的题怎么这么小清新

$n$ 个不相同的点,$q$ 组询问,每次给定 $l,r$,问在 $n$ 个点中,选出 $x$ 个点 $(x \in [l,r])$,用边连起来,能构成多少种不同的树

$n,q \leq 10^6$

sol:

首先知道 $n$ 个点的树有 $n^{n-2}$ 个,因为这题标号不同就算不同,所以 $i$ 个点不同的树有 $C_n^i \times i^{i-2}$

维护一下这东西的前缀和就可以每组询问 $O(1)$ 了

#include <bits/stdc++.h>
#define LL long long
using namespace std;
#define rep(i, s, t) for (register int i = (s), i##end = (t); i <= i##end; ++i)
#define dwn(i, s, t) for (register int i = (s), i##end = (t); i >= i##end; --i)
inline int read() {
int x = , f = ;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-')
f = -f;
for (; isdigit(ch); ch = getchar()) x = * x + ch - '';
return x * f;
}
const int maxn = 1e6 + ;
int n, T, mod, num[maxn], fac[maxn], ifac[maxn], cn[maxn], sum[maxn];
inline int ksm(int x, int t) {
if (t < )
return ;
if (t == )
return ;
int res = ;
for (; t; x = 1LL * x * x % mod, t = t >> )
if (t & )
res = 1LL * x * res % mod;
return res;
}
int main() {
n = read(), T = read(), mod = read();
rep(i, , n) num[i] = ksm(i, i - );
ifac[] = fac[] = ;
rep(i, , n) fac[i] = 1LL * fac[i - ] * i % mod;
ifac[n] = ksm(fac[n], mod - );
dwn(i, n - , ) ifac[i] = 1LL * ifac[i + ] * (i + ) % mod;
rep(i, , n) cn[i] = 1LL * (1LL * fac[n] * ifac[n - i] % mod) * ifac[i] % mod;
rep(i, , n) sum[i] = (sum[i - ] + (1LL * cn[i] * num[i] % mod)) % mod;
while (T--) {
int l = read(), r = read();
int ans = (((sum[r] - sum[l - ]) % mod) + mod) % mod;
printf("%d\n", ans);
}
}

loj #6216. 雪花挂饰的更多相关文章

  1. BZOJ4247挂饰

    Description     JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上.     JOI君的挂饰有一些与众不同--其中的一些挂饰附有可以挂其他挂件的挂钩 ...

  2. BZOJ 4247: 挂饰 题解

    Description JOI君有N个装在手机上的挂饰,编号为1...N. JOI君可以将其中的一些装在手机上. JOI君的挂饰有一些与众不同--其中的一些挂饰附有可以挂其他挂件的挂钩.每个挂件要么直 ...

  3. BZOJ 4247 挂饰 背包DP

    4247: 挂饰 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id ...

  4. bzoj千题计划197:bzoj4247: 挂饰

    http://www.lydsy.com/JudgeOnline/problem.php?id=4247 先把挂饰按挂钩数量从大到小排序 dp[i][j]前i个挂饰,剩下j个挂钩的最大喜悦值 分挂和不 ...

  5. BZOJ4247 : 挂饰

    首先将挂饰按照挂钩个数从大到小排序,然后DP 设f[i][j]处理完前i个挂饰,还有j个多余挂钩的最大喜悦值,则 f[0][1]=0 f[i][j]=max(f[i-1][max(j-a[i],0)+ ...

  6. 洛谷P4138 挂饰 背包

    正解:背包dp 解题报告: 昂先放链接qwq 感觉还挺妙的,,,真的我觉得我直接做可能是想不到背包的,,,我大概想不出是个背包的QAQ 但是知道是背包之后觉得,哦,好像长得也确实挺背包的吼,而且其实是 ...

  7. bzoj4247挂饰——压缩的动态规划

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4247 1.dp之前要先按挂钩个数从大到小排序,不然挂钩一度用成负的也可能是正确的,不仅脚标难 ...

  8. 【BZOJ4247】挂饰(动态规划)

    [BZOJ4247]挂饰(动态规划) 题面 BZOJ 题解 设\(f[i][j]\)表示前\(i\)个物品中还剩下\(j\)个挂钩时的最大答案. 转移显然是一个\(01\)背包,要么不选:\(f[i] ...

  9. [BZOJ4247]挂饰(DP)

    当最终挂饰集合确定了,一定是先挂挂钩多的在挂挂钩少的. 于是按挂钩从大到小排序,然后就是简单的01背包. #include<cstdio> #include<algorithm> ...

随机推荐

  1. c# 如何设置透明画刷

    使用solidBrush新建画刷,定义画刷的颜色为透明色 Brush b = new SolidBrush(Color.FromArgb(50, Color.Green)); 这里的50是透明度的设置 ...

  2. Kattis - convexpolygonarea 【数学】

    题意 给出一系列点,求这个多边形面积 思路 向量叉积 AC代码 #include <cstdio> #include <cstring> #include <ctype. ...

  3. 【leetcode刷题笔记】Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. 跨平台移动开发_PhoneGap API Camera 使用摄像头采集照片.

    camera对象提供对设备默认摄像头应用程序的访问. 程序运行效果 相关代码 <!DOCTYPE html> <html> <head> <title> ...

  5. IntelliJ IDEA 中 右键新建时,选项没有Java class的解决方法和具体解释

    我是在别人问我的时候,才发现还可以有这个问题,主要是他新项目直接打开,什么都没配置,就打算新建文件,其实可以直接看编辑器右下角的event log,那个对话框点开的话,可以add as maven p ...

  6. centos 源码安装php5.5

    系统环境: CentOS 6.5 / 7.0 x86_64 Fedora 20 x86_64下载 PHP 源码包 # wget http://cn2.php.net/distributions/php ...

  7. 【bzoj4401】块的计数(水dfs)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4401 假设把树划分为x个节点作一块,那么显然只有当x|n的时候才可能存在划分方案,并且这 ...

  8. centos下安装python2.7.9和pip以及数据科学常用的包

    以前一直用ubantu下的python,ubantu比较卡.自己倾向于使用centos,但默认的python版本太低,所以重新装了一个python和ipython centos6.5安装python2 ...

  9. 阅读linux内核代码的工具-- Source Insight

    http://blog.csdn.net/luckyaslan/article/details/7869235 Step 1:安装Source Insight并启动程序 可以进入图1界面,在工具条上有 ...

  10. javascript是一种面向对象语言吗?如果是,您在javascript中是如何实现继承的呢

    ·oop(面向对象程序设计)中最常用到的概念有 1.对象,属性,方法 1>(对象:具体事物或抽象事物,名词) 2>(属性:对象的特征,特点,形容词) 3>(方法:对象的动作,动词) ...