Codeforces Round #338 (Div. 2) 解题报告

2023-10-25 14:10

本文主要是介绍Codeforces Round #338 (Div. 2) 解题报告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

所以苟蒻万年不做E。。。


A:

A. Bulbs
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya wants to turn on Christmas lights consisting of m bulbs. Initially, all bulbs are turned off. There are n buttons, each of them is connected to some set of bulbs. Vasya can press any of these buttons. When the button is pressed, it turns on all the bulbs it's connected to. Can Vasya light up all the bulbs?

If Vasya presses the button such that some bulbs connected to it are already turned on, they do not change their state, i.e. remain turned on.

Input

The first line of the input contains integers n and m (1 ≤ n, m ≤ 100) — the number of buttons and the number of bulbs respectively.

Each of the next n lines contains xi (0 ≤ xi ≤ m) — the number of bulbs that are turned on by the i-th button, and then xi numbers yij(1 ≤ yij ≤ m) — the numbers of these bulbs.

Output

If it's possible to turn on all m bulbs print "YES", otherwise print "NO".

Sample test(s)
input
3 4
2 1 4
3 1 3 1
1 2
output
YES
input
3 3
1 1
1 2
1 1
output
NO
Note

In the first sample you can press each button once and turn on all the bulbs. In the 2 sample it is impossible to turn on the 3-rd lamp.


题意:

有n个开关和m个灯泡

每个开关按下后对应一些灯泡就会亮起来,而如果那些灯泡本来就是亮的那么不会改变状态

思路:

全部按一遍。。。随意模拟之

#include<iostream>
#include<cstdio>
#include<windows.h>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;const int maxn = 110;bool L[maxn];
int n,m,i,j;int main()
{#ifdef YZYfreopen("yzy.txt","r",stdin);#endifcin >> n >> m;memset(L,0,sizeof(L));for (i = 1; i <= n; i++) {scanf("%d",&j);while (j--) {int x;scanf("%d",&x);L[x] = 1;}}for (i = 1; i <= m; i++)if (!L[i]) {printf("NO");return 0;}printf("YES");return 0;
}


B. Longtail Hedgehog
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:

  1. Only segments already presented on the picture can be painted;
  2. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment;
  3. The numbers of points from the beginning of the tail to the end should strictly increase.

Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.

Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.

Input

First line of the input contains two integers n and m(2 ≤ n ≤ 100 0001 ≤ m ≤ 200 000) — the number of points and the number segments on the picture respectively.

Then follow m lines, each containing two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.

Output

Print the maximum possible value of the hedgehog's beauty.

Sample test(s)
input
8 6
4 5
3 5
2 5
1 2
2 8
6 7
output
9
input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
output
12
Note

The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 12 and 5. The following segments are spines: (25), (35) and (45). Therefore, the beauty of the hedgehog is equal to 3·3 = 9.


题意:

Masha 有一张n个点m条边的图有如下几种定义:

1.刺猬:

由一些点构成,包括一条尾巴和刺,当然他有一个中心,如例图中的5号点

2.刺:

每个点的刺的数量等于它的度数

3.尾巴:

由某个点到刺猬中心的路径上的所有点,尾巴大小等于点的数量

并且尾巴上的点的编号必须严格递增

问:

任选一个点做刺猬的中心,该刺猬的刺的数量*尾巴大小的最大值是多少?

思路:

对于刺,维护度数即可

对于尾巴长度,每给出一条边就把它想成从编号小的点连向编号大的点的有向边,让编号大的点的入度+1。显然这样做的图不存在环!

那么只需对原图进行一次拓扑排序,维护当前到这个点能够成的最长尾巴长度,就能统计出来啦

注:尾巴长度可以等于1。。。苟蒻真是英语太差。。。


#include<iostream>
#include<cstdio>
#include<windows.h>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;const int maxn = 1E5 + 10;int L[maxn],d[maxn],s[maxn],n,m;
bool vis[maxn];vector <int> v[maxn];
queue <int> q;void BFS(int u)
{q.push(u);while (!q.empty()) {int k = q.front(); q.pop();for (int l = 0; l < v[k].size(); l++) {int to = v[k][l];--d[to];if (to < k) continue;vis[to] = 1;L[to] = max(L[to],L[k] + 1);if (!d[to]) q.push(to);}}
}int main()
{#ifdef YZYfreopen("yzy.txt","r",stdin);#endifcin >> n >> m;while (m--) {int x,y;scanf("%d%d",&x,&y);++d[max(x,y)];++s[x]; ++s[y];v[x].push_back(y);v[y].push_back(x);}memset(L,0,sizeof(L));memset(vis,0,sizeof(vis));for (int i = 1; i <= n; i++)if (!vis[i]) {L[i] = 1; vis[i] = 1;BFS(i);}long long ans = 0;for (int i = 1; i <= n; i++) //if (L[i] > 1) ans = max(ans,1LL*L[i]*1LL*s[i]);cout << ans;return 0;
}

C. Running Track
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.

First, he wants to construct the running track with coating t. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.

Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings s. Also, he has scissors and glue. Ayrat is going to buy some coatings s, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating s he needs to buy in order to get the coating t for his running track. Of course, he also want's to know some way to achieve the answer.

Input

First line of the input contains the string s — the coating that is present in the shop. Second line contains the string t — the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100.

Output

The first line should contain the minimum needed number of coatings n or -1 if it's impossible to create the desired coating.

If the answer is not -1, then the following n lines should contain two integers xi and yi — numbers of ending blocks in the corresponding piece. If xi ≤ yi then this piece is used in the regular order, and if xi > yi piece is used in the reversed order. Print the pieces in the order they should be glued to get the string t.

Sample test(s)
input
abc
cbaabc
output
2
3 1
1 3
input
aaabrytaaa
ayrat
output
3
1 1
6 5
8 7
input
ami
no
output
-1
Note

In the first sample string "cbaabc" = "cba" + "abc".

In the second sample: "ayrat" = "a" + "yr" + "at".


题意:

Ayrat想做一个字符串问题。。。

现有一个串a还有一个串b

每次能得到一个串a,从中选取一个子串(可以把a翻转然后再取)把它接在之前拼的串后面,最终要得到串b

问最少需要几个串a???

思路:

显然dp

f[i] = min(f[j]+1) 该方程可行条件当b串[j+1,i]位置是a的子串

如果能O(1)判断是不是的话就能O(n^2)解决了!

那么将a串的所有子串都扔进一个trie,每个子串长度为n,一共有n^2个子串。。。

所以空间复杂度是O(n^3)

所以因为算错空间复杂度纠结了十几分钟。。。。。。。。。。

可以想象一开始从位置1到位置strlen(b),每次固定起点,右指针从左往右扫一遍就把所有以i为起点的子串扔进trie了,这样做每次消耗空间O(n),所以总的空间复杂度O(n^2)

dp过程就简单了!

注:

烧空间大小一开始没算好,然后打的时候没注意小写英文字母问题,所有'a'打成'A'。。。。。。

#include<iostream>
#include<cstdio>
#include<windows.h>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;const int maxn = 2110;
const int INF = 10000;struct T{int s[26];short l,r;
}trie[maxn*maxn];char a[maxn],b[maxn];
int cur = 0,n,m,f[maxn],fa[maxn],l[maxn],r[maxn];void b1(int i)
{int now = 0;int x = i;for (; i <= n; i++) {if (!trie[now].s[a[i]-'a']) trie[now].s[a[i]-'a'] = ++cur;now = trie[now].s[a[i]-'a'];trie[now].l = x;trie[now].r = i;}
}void b2(int i)
{int now = 0;int x = i;for (; i > 0; i--) {if (!trie[now].s[a[i]-'a']) trie[now].s[a[i]-'a'] = ++cur;now = trie[now].s[a[i]-'a'];trie[now].l = x;trie[now].r = i;}
}void P(int k)
{if (!fa[k]) {cout << l[k] << ' ' << r[k] << endl;return;}P(fa[k]);cout << l[k] << ' ' << r[k] << endl;
}int main()
{#ifdef YZYfreopen("yzy.txt","r",stdin);#endifscanf("%s",1+a);scanf("%s",1+b);n = m = 0;for (int i = 1; a[i] >= 'a' && a[i] <= 'z'; i++,n++);for (int i = 1; b[i] >= 'a' && b[i] <= 'z'; i++,m++);for (int i = 1; i <= n; i++) b1(i);for (int i = n; i > 0; i--) b2(i);for (int i = 1; i <= m; i++) f[i] = INF;f[0] = 0;for (int i = 1; i <= m; i++) {int now = 0;for (int j = i; j > 0; j--) {if (trie[now].s[b[j]-'a']) {now = trie[now].s[b[j]-'a'];if (f[i] > f[j-1] + 1) {f[i] = f[j-1] + 1;l[i] = trie[now].r;r[i] = trie[now].l;fa[i] = j-1;}}else break;}}if (f[m] == INF) cout << -1;else {cout << f[m] << endl;P(m); }return 0;
}

D. Multipliers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ayrat has number n, represented as it's prime factorization pi of size m, i.e. n = p1·p2·...·pm. Ayrat got secret information that that the product of all divisors of n taken modulo 109 + 7 is the password to the secret data base. Now he wants to calculate this value.

Input

The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of primes in factorization of n.

The second line contains m primes numbers pi (2 ≤ pi ≤ 200 000).

Output

Print one integer — the product of all divisors of n modulo 109 + 7.

Sample test(s)
input
2
2 3
output
36
input
3
2 3 2
output
1728
Note

In the first sample n = 2·3 = 6. The divisors of 6 are 123 and 6, their product is equal to 1·2·3·6 = 36.

In the second sample 2·3·2 = 12. The divisors of 12 are 12346 and 121·2·3·4·6·12 = 1728.


题意:

给n个质数,记x 为他们的乘积

问x所有因数的乘积是多少

思路:

场内做到这题时只剩5min了。。。。。

然而全是没学过的数学知识。。。

1.

一个数x记分解质因数后有

x = p1^a1*p2^a2*p3^a3*...*pn^an

则它的因数个数为f(x) = (a1+1)*(a2+1)*(a3+1)*...*(an+1)

2.费马小定理

a^(m-1) ≡ 1(mod m) ==> a^x ≡ a^(x%(m-1)) (mod m)

当m是素数且a != 0。。。

对于某非完全平方数x,由于因数总是成对出现,于是可以把他们两两配对

于是 ans = x^(f(x)/2)

反之,因数会多出一个可爱的根号x

于是 ans = x^((f(x)-1)/2)*根号x

显然可以通过质因数个数判断该x是否为完全平方数

如果是,那么x因数个数必为奇数,mod 10E9+6后还是奇数 所以可以通过取余结束强行除以2来确定x的指数 然后根号x就是一半质因数乘起来

反之,可以在统计f(x)过程任取ai%2 == 1,在这个地方先除以2

综上,因为2在模10E9+6系里面无法求出逆元来着。。。

所以用一些投机取巧的办法(codeforces上的题解真心看不懂。。。。。)

#include<iostream>
#include<cstdio>
#include<windows.h>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;const int maxn = 2E5 + 20;
typedef long long LL;
const LL mo = 1000000007;LL p = 1,x = 1,ans = 1,y = 1,tot = 1;
int num[maxn],n;LL ksm(LL a,LL b)
{LL ret = 1;for (; b; b >>= 1) {if (b & 1) ret = ret * a % mo;a = a*a%mo;}return ret%mo;
}int main()
{#ifdef YZYfreopen("yzy.txt","r",stdin);#endifcin >> n;for (int i = 1; i <= n; i++) {int j;scanf("%d",&j);++num[j];}int flag = 1;for (int i = 2; i <= 200000; i++)if (num[i]) {x = x*ksm(i,num[i]/2)%mo;if (num[i] % 2 == 1 && flag) {p = p*(1LL*(num[i]+1)/2LL) % (mo-1LL);flag = 0;}else {p = p*1LL*(num[i]+1)%(mo-1);}tot = tot*ksm(i,num[i])%mo;}if (flag) p /= 2,ans = ans*x%mo;ans = ans*ksm(tot,p)%mo;cout << ans;return 0;
}


这篇关于Codeforces Round #338 (Div. 2) 解题报告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/283109

相关文章

【专题】2024飞行汽车技术全景报告合集PDF分享(附原数据表)

原文链接: https://tecdat.cn/?p=37628 6月16日,小鹏汇天旅航者X2在北京大兴国际机场临空经济区完成首飞,这也是小鹏汇天的产品在京津冀地区进行的首次飞行。小鹏汇天方面还表示,公司准备量产,并计划今年四季度开启预售小鹏汇天分体式飞行汽车,探索分体式飞行汽车城际通勤。阅读原文,获取专题报告合集全文,解锁文末271份飞行汽车相关行业研究报告。 据悉,业内人士对飞行汽车行业

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

Python:豆瓣电影商业数据分析-爬取全数据【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】

**爬取豆瓣电影信息,分析近年电影行业的发展情况** 本文是完整的数据分析展现,代码有完整版,包含豆瓣电影爬取的具体方式【附带爬虫豆瓣,数据处理过程,数据分析,可视化,以及完整PPT报告】   最近MBA在学习《商业数据分析》,大实训作业给了数据要进行数据分析,所以先拿豆瓣电影练练手,网络上爬取豆瓣电影TOP250较多,但对于豆瓣电影全数据的爬取教程很少,所以我自己做一版。 目

开题报告中的研究方法设计:AI能帮你做什么?

AIPaperGPT,论文写作神器~ https://www.aipapergpt.com/ 大家都准备开题报告了吗?研究方法部分是不是已经让你头疼到抓狂? 别急,这可是大多数人都会遇到的难题!尤其是研究方法设计这一块,选定性还是定量,怎么搞才能符合老师的要求? 每次到这儿,头脑一片空白。 好消息是,现在AI工具火得一塌糊涂,比如ChatGPT,居然能帮你在研究方法这块儿上出点主意。是不

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor