Recently, TeaTree acquire new knoledge gcd (Greatest Common Divisor), now she want to test you.
As we know, TeaTree is a tree and her root is node 1, she have n nodes and n-1 edge, for each node i, it has it’s value v[i].

For every two nodes i and j (i is not equal to j), they will tell
their Lowest Common Ancestors (LCA) a number : gcd(v[i],v[j]).

For each node, you have to calculate the max number that it heard. some definition:

In graph theory and computer science, the lowest common ancestor
(LCA) of two nodes u and v in a tree is the lowest (deepest) node that
has both u and v as descendants, where we define each node to be a
descendant of itself.
InputOn the first line, there is a positive integer n, which describe the number of nodes.

Next line there are n-1 positive integers f[2] ,f[3], …, f[n], f[i] describe the father of node i on tree.

Next line there are n positive integers v[2] ,v[3], …, v[n], v[i] describe the value of node i.

n<=100000, f[i]<i, v[i]<=100000OutputYour output should include n lines, for i-th line, output the max number that node i heard.

For the nodes who heard nothing, output -1.Sample Input

4
1 1 3
4 1 6 9

Sample Output

2
-1
3
-1
 
题意 : 给你一颗树,每个结点上面都有一个权值,询问你一任意一个结点作为树根其子树上任意两点的 gcd 的最大值是多少
思路分析 :
  1 . 用 vector 去模拟归并排序,当合并时遇到相同的就记录一下最大值
#define ll long long
const int maxn = 1e5+5; int n;
vector<int>d[maxn], ve[maxn], f[maxn];
void init(){
for(int i = 1; i <= 100000; i++){
for(int j = i; j <= 100000; j += i){
d[j].push_back(i);
}
}
}
int val[maxn], ans[maxn];
vector<int>temp; void merge(int x, int y){
temp.clear();
if (f[x].size() == 0) f[x] = d[val[x]];
if (f[y].size() == 0) f[y] = d[val[y]]; int i = 0, j = 0;
while((i < f[x].size()) && (j < f[y].size())){
if (f[x][i] < f[y][j]) temp.push_back(f[x][i]), i++;
else if (f[x][i] > f[y][j]) temp.push_back(f[y][j]), j++;
else {
temp.push_back(f[x][i]);
ans[x] = max(ans[x], f[x][i]); i++, j++;
}
}
while (i < f[x].size()) {
temp.push_back(f[x][i]);
i++;
}
while(j < f[y].size()) {
temp.push_back(f[y][j]);
j++;
}
f[x].clear(); f[y].clear();
f[x] = temp;
} void dfs(int x){
for(int i = 0; i < ve[x].size(); i++){
int to = ve[x][i];
dfs(to);
merge(x, to);
}
} int main() {
init();
cin >> n; int x;
memset(ans, -1, sizeof(ans));
for(int i = 2; i <= n; i++){
scanf("%d", &x);
ve[x].push_back(i);
}
for(int i = 1; i <= n; i++){
scanf("%d", &val[i]);
}
dfs(1);
for(int i = 1; i <= n; i++) printf("%d\n", ans[i]);
return 0;
}

2 .

 
Count the number of cyclic permutations of length n with no continuous subsequence [i, i + 1 mod n].
Output the answer modulo 998244353.
InputThe first line of the input contains an integer T , denoting the number of test cases.

In each test case, there is a single integer n in one line, denoting the length of cyclic permutations.

1 ≤ T ≤ 20, 1 ≤ n ≤ 100000OutputFor each test case, output one line contains a single integer, denoting the answer modulo 998244353.Sample Input

3
4
5
6

Sample Output

1
8
36 题意 : 有一个循环全排列,求相邻的位置不存在 [i, i+1] 以及 [n, 1] 的排列的方案数有多少个?
思路分析 :
    好菜啊..学的假的组合数学吧....
 

首先先说明什么是循环排列:

即把1-n这n个数随意地放到一个圆圈上,循环排列的不同仅仅取决于这n个数的相对位置的不同。

例如1234,2341,3412,4123这些数为相同的循环排列数。

循环排列没有首末之分,这四个元素随便从哪一个元素开始,绕一个方向转过去,都不改变它们的相对顺序;直线排列则首末分明,原来排末位,调换排首位,已改变它们的相对顺序。循环排列与直线排列的主要区别就在这一点上。

从例子看出,直线排列的个数是循环排列个数的n倍

由直线排列个数为n!可推知循环排列个数为(n-1)!。

讲完了循环排列,再来看此题是求不含子串[i,i+1]或[n,1](以下简称顺序子串,共有n个)的循环排列个数

因为一个排列中可能含有多个顺序子串,所以我们列举至少含有0个,1个,...n个的情况  (注意是至少,因为无法保证恰好含有i个)

包含至少一个顺序子串的循环排列数为C(n,1)*(n-2)!

包含至少两个顺序子串的循环排列数为C(n,2)*(n-3)!

...

包含至少k个顺序子串的循环排列数为C(n,k)*(n-k-1)!

(为什么是(n-k-1)! 当你选出了k个子串之后,至少有k+1个数相对位置已被确定,我们让剩下的(n-k-1)个数全排列即可。)

同时注意到包含n个顺序子串的循环排列数一定是1个。

事件之间相互包含,所以用到容斥原理:

∑(k从0到n-1)(-1)^k*C(n,k)*(n-k-1)!+(-1)^n*1

2018 Multi-University Training Contest 10的更多相关文章

  1. 2018 Nowcoder Multi-University Training Contest 10

    Practice Link J. Rikka with Nickname 题意: 给出\(n\)个字符串,要求依次合并两个串\(s, t\),满足将\(t\)合并到\(s\)中变成\(r\),使得\( ...

  2. 2016 Multi-University Training Contest 10

    solved 7/11 2016 Multi-University Training Contest 10 题解链接 分类讨论 1001 Median(BH) 题意: 有长度为n排好序的序列,给两段子 ...

  3. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)

    CRB and Tree                                                             Time Limit: 8000/4000 MS (J ...

  4. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  6. [二分,multiset] 2019 Multi-University Training Contest 10 Welcome Party

    Welcome Party Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)T ...

  7. 2015 Multi-University Training Contest 10(9/11)

    2015 Multi-University Training Contest 10 5406 CRB and Apple 1.排序之后费用流 spfa用stack才能过 //#pragma GCC o ...

  8. 2018 Multi-University Training Contest 10 Solution

    A - Problem A.Alkane 留坑. B - Problem B. Beads 留坑. C - Problem C. Calculate 留坑. D - Problem D. Permut ...

  9. HDU - 6430 Problem E. TeaTree 2018 Multi-University Training Contest 10 (LCA+枚举因子)

    题意:一棵树,每个点都有自己val(1 <= val <= 1e5),而任意两个点u,v可以对lca(u,v) 产生gcd(valu,valv)的贡献,求每个点能接受到来自子树贡献的最大值 ...

  10. HDU - 6435 Problem J. CSGO 2018 Multi-University Training Contest 10 (二进制枚举+思维)

    题意:有N个主武器(MW)和M个副武器(SW),每个武器都有自己的S值,和K个附加属性xi.要选取一对主副武器搭配,搭配后获得的性能由该公式得出: 求获得最大的性能为多少. 分析:由于|xm - xs ...

随机推荐

  1. vscode编辑如何保存时自动校准eslint规范

    在日常开发中,一个大点的项目会有多人参与,那么可能就会出现大家的代码风格不一,各显神通,这个时候就要祭出我们的eslint. 在这之前磨刀不误砍柴工,我们先来配置一下我们的代码编辑工具,如何在vsco ...

  2. [转]ASP.NET WebApi OWIN 实现 OAuth 2.0

    OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. OAuth 允许用户提供一个令牌, ...

  3. 2019-10-10-优雅调试-REST-API-的工具

    title author date CreateTime categories 优雅调试 REST API 的工具 lindexi 2019-10-10 20:9:33 +0800 2019-10-1 ...

  4. UPC 2019年第二阶段我要变强个人训练赛第六场

    传送门 A.上学路线 题目描述 小D从家到学校的道路结构是这样的:由n条东西走向和m条南北走向的道路构成了一个n*m的网格,每条道路都是单向通行的(只能从北向南,从西向东走). 已知小D的家在网格的左 ...

  5. 获取 Nuget 版本号

    本文告诉大家通过命令行获取 Nuget 的版本号 在 Nuget 中没有 -version 和 -v 和 --version 等写法,只需要直接输入 nuget 在第一行就会显示版本号 nuget N ...

  6. linux I/O 内存分配和映射

    I/O 内存区必须在使用前分配. 分配内存区的接口是( 在 <linux/ioport.h> 定义): struct resource *request_mem_region(unsign ...

  7. ie6 ie7下,Li不能自动换行,出现竖排文字现象(PS:li不固定宽度,所有li同一行显示),在ie8却可以

    好久没写CSS,今天又发现一个ie兼容问题. 我需要所有的li在同一行显示,不固定Li的宽度,如果一行排不下,需要自动换行.当然Li的内容长度不同. 必须在li加white-space:nowrap; ...

  8. slim中返回结果加密的

    //返回结果不加密 $this->get("/open]",function (Request $request, Response $response, $args) {  ...

  9. 使用Sklearn-train_test_split 划分数据集

    使用sklearn.model_selection.train_test_split可以在数据集上随机划分出一定比例的训练集和测试集 1.使用形式为: from sklearn.model_selec ...

  10. JMeter录制登录测试

    本节试图解释使用任何公开可用的网站记录登录测试的确切步骤,该网站提供具有登录凭据的可靠登录页面. 出于测试目的,我们将使用OrangeHRM在URL- http://opensource.demo.o ...