比赛时候切了A-E,fst了A

Standings第一页只有三个人挂了A题,而我就是其中之一,真™开心啊蛤蛤蛤

A. Fake NP

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

Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path.

You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of times.

Solve the problem to show that it's not a NP problem.

Input

The first line contains two integers l and r (2 ≤ l ≤ r ≤ 109).

Output

Print single integer, the integer that appears maximum number of times in the divisors.

If there are multiple answers, print any of them.

Examples
input
19 29
output
2
input
3 6
output
3
Note

Definition of a divisor: https://www.mathsisfun.com/definitions/divisor-of-an-integer-.html

The first example: from 19 to 29 these numbers are divisible by 2: {20, 22, 24, 26, 28}.

The second example: from 3 to 6 these numbers are divisible by 3: {3, 6}.

脑洞题

求被[l,r]范围的数拥有的最多的因数。

如果l==r就输出l,如果l<r就输出2,正确性显然

只要5行就可以AC呢

 #include<iostream>
#include<cstdio>
using namespace std;
int main(){
int l,r;
scanf("%d%d",&l,&r);
if(l==r)printf("%d\n",l);
else printf("2\n");
return ;
}

看到题的时候蠢兮兮枚举sqrt范围内的数,算1~r的贡献减去1~l-1的贡献。

其实这样也可以龟速跑过的吧?但是减的时候减了1~l的贡献,PP以后还自信锁题试图hack别人,这就回天无力了。(PP都是骗人的)

B. 3-palindrome
time limit per test  1 second
memory limit per test  256 megabytes
input standard input
output standard output

In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.

He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings "abc" and "abca" suit him, while the string "aba" doesn't. He also want the number of letters 'c' in his string to be as little as possible.

Input

The first line contains single integer n (1 ≤ n ≤ 2·105) — the length of the string.

Output

Print the string that satisfies all the constraints.

If there are multiple answers, print any of them.

Examples
input
2
output
aa
input
3
output
bba
Note

A palindrome is a sequence of characters which reads the same backward and forward.

看上去是个贪心递推?假的!

abbaabbaabba无限循环即可

或者"aabb"也可以

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
char s[mxn];
int n;
bool check(int i,char c){
if(s[i-]==c)return ;
return ;
}
int main(){
int i,j;
n=read();
for(i=;i<=n;i++){
if(i%==)printf("a");
else if(i%==)printf("b");
else if(i%==)printf("b");
else if(i%==)printf("a");
}
printf("\n");
return ;
}

B

C. Find Amir

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

A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.

There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs  and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of schools.

Output

Print single integer: the minimum cost of tickets needed to visit all schools.

Examples
input
2
output
0
input
10
output
4
Note

In the first example we can buy a ticket between the schools that costs .

贪心 构造

构造题真开心

代价是$ \mod (n+1) $意义下计算的,脑洞一下可以发现1 to n , 2 to n-1 3 to n-2 这样的走法代价为0,而n to 2,n-1 to 3这样的转移代价是1

于是这样转转转就可以了,再考虑到总共有奇数个点的情况,答案为(n+1)/2-1

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int main(){
int i,j;
int n=read();
if(n==)printf("0\n");
else{
printf("%d\n",(n+)/-);
}
return ;
}

C

D. Minimum number of steps

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

We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.

The string "ab" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.

Input

The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.

Output

Print the minimum number of steps modulo 109 + 7.

Examples
input
ab
output
1
input
aab
output
3
Note

The first example: "ab"  →  "bba".

The second example: "aab"  →  "abba"  →  "bbaba"  →  "bbbbaa".

找规律

发现如果有连续的一串a后面一个b,它们对答案的贡献是$(2^cnt[a])-1$

cnt是要累积的,因为前面的ab换成bba以后,如果后面还有b,仍然对答案有贡献

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int mod=1e9+;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int ksm(int a,int k){
int res=;
while(k){
if(k&)res=(LL)res*a%mod;
a=(LL)a*a%mod;
k>>=;
}
return res;
}
char s[mxn];
int main(){
int i,j;
scanf("%s",s+);
int n=strlen(s+);
int ans=,cnt=;
for(i=;i<=n;i++){
if(s[i]=='a'){
cnt++;
}
else if(cnt){
ans=((LL)ans+ksm(,cnt)%mod-)%mod;
// cnt--;
}
}
printf("%d\n",ans);
return ;
}

D

E. Ice cream coloring

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

Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?"

We have a tree T with n vertices and m types of ice cream numerated from 1 to m. Each vertex i has a set of si types of ice cream. Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a connected subgraph. We build a new graph G with m vertices. We put an edge between the v-th and the u-th (1 ≤ u, v ≤ mu ≠ v) vertices in G if and only if there exists a vertex in T that has both the v-th and the u-th types of ice cream in its set. The problem is to paint the vertices of G with minimum possible number of colors in a way that no adjacent vertices have the same color.

Please note that we consider that empty set of vertices form a connected subgraph in this problem.

As usual, Modsart don't like to abandon the previous problem, so Isart wants you to solve the new problem.

Input

The first line contains two integer n and m (1 ≤ n, m ≤ 3·105) — the number of vertices in T and the number of ice cream types.

n lines follow, the i-th of these lines contain single integer si (0 ≤ si ≤ 3·105) and then si distinct integers, each between 1 and m — the types of ice cream in the i-th vertex. The sum of si doesn't exceed 5·105.

n - 1 lines follow. Each of these lines describes an edge of the tree with two integers u and v (1 ≤ u, v ≤ n) — the indexes of connected by this edge vertices.

Output

Print single integer c in the first line — the minimum number of colors to paint the vertices in graph G.

In the second line print m integers, the i-th of which should be the color of the i-th vertex. The colors should be between 1 and c. If there are some answers, print any of them.

Examples
input
3 3
1 1
2 2 3
1 2
1 2
2 3
output
2
1 1 2
input
4 5
0
1 1
1 3
3 2 4 5
2 1
3 2
4 3
output
3
1 1 1 2 3
Note

In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors.

In the second example the colors of the second, the fourth and the fifth ice cream should obviously be distinct.

贪心 构造 读题

开这题的时候还有1h10min,看了几遍题面没看懂,觉得接下来一个小时都要砸这题上了。

转去看F,觉得如果做不出E那肯定也做不出F。

这时候我注意到我的A题有BUG,但是我已经把它锁了。

实时rank900+,如果不再肝一道题出来就是大幅掉分预定

单纵就是干!斩杀靠砸桶!(大雾)

懵逼了半小时,然后敲了个乱搞代码居然真的乱搞出来了,蛤蛤蛤

这题卡读题啊……看那个我标成红色的句子,大致意思就是说,同种标号的冰淇淋挤在树的连通块上,它是解题的关键。

注意到每个树结点内包含的冰淇淋构成一个完全图,它们的颜色必定不一样。

脑洞一下可以得出最少需要的颜色数是max{一个结点包含的冰淇淋数}

由于上面那个性质,暴力DFS贪心染色不会有问题

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
int n,m,ans;
int col[mxn];
vector<int>ve[mxn];
bool vis[mxn];
int st[mxn],top=;
void DFS(int u,int fa){
int cnt=,t;top=;
for(int i=;i<ve[u].size();i++){
t=ve[u][i];
if(col[t])st[++top]=t,vis[col[t]]=;
}
for(int i=;i<ve[u].size();i++){
t=ve[u][i];
if(!col[t]){
while(vis[cnt])cnt++;
col[t]=cnt;
cnt++;
}
}
while(top)vis[col[st[top--]]]=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==fa)continue;
DFS(v,u);
}
return;
}
int main(){
int i,j,s,u,v;
n=read();m=read();
ans=;
for(i=;i<=n;i++){
s=read();
ans=max(ans,s);
for(j=;j<=s;j++)
ve[i].push_back(read());
}
for(i=;i<n;i++){
u=read();v=read();
add_edge(u,v);add_edge(v,u);
}
DFS(,);
printf("%d\n",ans);
for(i=;i<=m;i++)if(!col[i])col[i]=;
for(i=;i<=m;i++){
printf("%d ",col[i]);
}
return ;
}

E

F. Expected diameter of a tree

time limit per test  3 seconds
memory limit per test  256 megabytes
input standard input
output standard output

Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem.

We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the set of vertices in the connected component of the graph that contains v, and U be the set of vertices in the connected component of the graph that contains u. Let's add an edge between some vertex  and some vertex in  and compute the value d of the resulting component. If the resulting component is a tree, the value d is the diameter of the component, and it is equal to -1 otherwise. What is the expected value of d, if we choose vertices a and b from the sets uniformly at random?

Can you help Pasha to solve this problem?

The diameter of the component is the maximum distance among some pair of vertices in the component. The distance between two vertices is the minimum number of edges on some path between the two vertices.

Note that queries don't add edges to the initial forest.

Input

The first line contains three integers nm and q(1 ≤ n, m, q ≤ 105) — the number of vertices, the number of edges in the graph and the number of queries.

Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n), that means there is an edge between vertices ui and vi.

It is guaranteed that the given graph is a forest.

Each of the next q lines contains two integers ui and vi (1 ≤ ui, vi ≤ n) — the vertices given in the i-th query.

Output

For each query print the expected value of d as described in the problem statement.

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if .

Examples
input
3 1 2
1 3
3 1
2 3
output
-1
2.0000000000
input
5 2 3
2 4
4 3
4 2
4 1
2 5
output
-1
2.6666666667
2.6666666667
Note

In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2.

In the second example the answer for the first query is obviously -1. The answer for the second query is the average of three cases: for added edges 1 - 2 or 1 - 3 the diameter is 3, and for added edge 1 - 4 the diameter is 2. Thus, the answer is .

这题太菜啦!我只用了整整一上午时间就写出来啦!

数学问题 数学期望 树形DP 并查集

用并查集可以判连通块,并且搞出连通块的size

树形DP可以处理出每个点出发能走的最远距离g[](用于计算直径)

询问两个点的时候,如果在同一个连通块里输出-1 (废话)

否则找到这两个点对应的树。假设在两棵树上分别选一点得到u,v,如果g[u]+g[v]+1大于原树直径,那么贡献就是g[u]+g[v]+1,否则贡献是原树直径。

计算每种方案的贡献res, $ans=res/(size[find(u)]*size[find(v)])$

将同一棵树的g[]从小到大排序以后用two pointers扫描可以优化上述过程,做到$ O(n) $计算,或者排序后二分$O(nlogn)$

再加个记忆化就能过了

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
const double eps=1e-;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v){
e[++mct].v=v;e[mct].nxt=hd[u];hd[u]=mct;return;
}
int fa[mxn],sz[mxn];
int vis[mxn];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
//
vector<int> D[mxn],smm[mxn];
int dis[mxn],g[mxn];
#define amax(a,b) ((a)>(b))?(a):(b)
void DFS(int u,int ff){
for(int i=hd[u],v;i;i=e[i].nxt){
v=e[i].v; if(v==ff)continue;
DFS(v,u);
dis[u]=amax(dis[u],dis[v]+);
}
return;
}
inline void update(int *a,int x){
for(int i=;i<;i++)
if(x>a[i])swap(a[i],x);
return;
}
void DFS2(int u,int ff,int up,vector<int> &now){
int ano[];//通往u的其他子树的最长/次长链
ano[]=ano[]=-;
vis[u]=;
g[u]=amax(dis[u],up);//从u出发能走的最远距离
now.push_back(g[u]);
//
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;if(v==ff)continue;
update(ano,dis[v]);
}
for(int i=hd[u],v;i;i=e[i].nxt){
v=e[i].v;if(v==ff)continue;
int tmp=(ano[]==dis[v])?ano[]:ano[];
DFS2(v,u,amax(tmp+,up+),now);
}
return;
} #undef amax
int n,m,Q;
map<pair<int,int>,double>mp;
void calc(int u,int v){
if(mp.count(make_pair(u,v))){
printf("%.10f\n",mp[make_pair(u,v)]);
return;
}
if(sz[u]<sz[v])swap(u,v);
int L=max(D[u].back(),D[v].back());
double res=0.0;
int K=D[u].size();
for(int i=;i<D[v].size();i++){
int x=upper_bound(D[u].begin(),D[u].end(),L-D[v][i]-)-D[u].begin();
res+=1.0*x*L+(smm[u][K]-smm[u][x])+1.0*(K-x)*(D[v][i]+);
}
res/=D[u].size();res/=D[v].size();
if(u>v)swap(u,v);
mp[make_pair(u,v)]=res;
printf("%.10f\n",res);
return;
}
int main(){
int i,j,u,v;
n=read();m=read();Q=read();
for(i=;i<=n;i++)fa[i]=i,sz[i]=;
for(i=;i<=m;i++){
u=read();v=read();
add_edge(u,v);add_edge(v,u);
u=find(u);v=find(v);
if(u==v)continue;
fa[v]=u;
sz[u]+=sz[v];
}
for(i=;i<=n;i++)
if(find(i)==i)DFS(i,);
for(i=;i<=n;i++){
if(!vis[i]){
int x=find(i);
DFS2(x,,,D[x]);
sort(D[x].begin(),D[x].end());
int ed=D[x].size();
smm[x].resize(ed+);
for(j=;j<=ed;j++)
smm[x][j]=smm[x][j-]+D[x][j-];
}
}
while(Q--){
u=read();v=read();
u=find(u);v=find(v);
if(u==v){
printf("-1\n");
continue;
}
if(u>v)swap(u,v);
calc(u,v);
}
return ;
}

F

Codeforces Round #411 (Div. 2) A-F的更多相关文章

  1. Codeforces Round #573 (Div. 1) 差F

    Codeforces Round #573 (Div. 1) E 题意:二维平面上有 n 个点,你可以放至多 m 条直线使得 (0,0) 与每个点的连线至少与一条直线相交.求原点与所有直线的距离最小值 ...

  2. Codeforces Round #541 (Div. 2) (A~F)

    目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...

  3. Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)

    F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...

  4. Codeforces Round #600 (Div. 2)E F

    题:https://codeforces.com/contest/1253/problem/E 题意:给定n个信号源,俩个参数x和s,x代表这个信号源的位置,s代表这个信号源的波及长度,即这个信号源可 ...

  5. Codeforces Round #346 (Div. 2) E F

    因为很久没有个人认真做题了 昨天晚上开了场虚拟cf来锻炼个人手速 选的是第一次做cf的场 那时候7出3还被hack...之后也没补题 这次做的时候顺便回忆了一下以前比赛的时候是怎么想的 发现经验还是很 ...

  6. Codeforces Round #411 (Div. 2)(A,B,C,D 四水题)

    A. Fake NP time limit per test:1 second memory limit per test:256 megabytes input:standard input out ...

  7. Codeforces Round #411 (Div. 1) D. Expected diameter of a tree

    题目大意:给出一个森林,每次询问给出u,v,问从u所在连通块中随机选出一个点与v所在连通块中随机选出一个点相连,连出的树的直径期望(不是树输出-1).(n,q<=10^5) 解法:预处理出各连通 ...

  8. Codeforces Round #411 (Div. 2)

    来自FallDream的博客,未经允许,请勿转载,谢谢. 由于人傻又菜 所以这次又滚去div2了  一堆结论题真的可怕 看见E题不是很有思路   然后就去大力搞F题  T了最后一个点 真的绝望   但 ...

  9. Codeforces Round #322 (Div. 2) E F

    E. Kojiro and Furrari 题意说的是 在一条直线上 有n个加油站, 每加一单位体积的汽油 可以走1km 然后每个加油站只有一种类型的汽油,汽油的种类有3种 求从起点出发到达终点要求使 ...

随机推荐

  1. 计算器软件实现系列(七)WPF+SQL+策略模式

    一  整体概述 本次设计主要是在WPF的页面中实现的,属于表现层的更换,数据库部分用的还是数据库的封装,其中引用了策略模式 二  设计思路 1 在出题页面,进行试题的编辑,在编辑后会自动保存到数据库中 ...

  2. P4编程环境搭建

    本文参照了sdnlab上相关文章的搭建推荐. 使用的系统环境为ubuntu 18.04 组件介绍 主要安装五个组件: BMv2:是一款支持P4编程的软件交换机 p4c:是一款P4的编译器 PI:是P4 ...

  3. 实验吧编程题:Hashkill

    原题:6ac66ed89ef9654cf25eb88c21f4ecd0是flag的MD5码,(格式为ctf{XXX_XXXXXXXXXXX_XXXXX})由一个0-1000的数字,下划线,纽约的一个区 ...

  4. dedecms 后台登录地址

    dedecms  后台登录地址 http://www.域名.com/member/index.php

  5. 【bzoj5001】搞事情 暴力

    题目描述 给定一个NM的01矩阵,每次可以选定一个位置,将它和它相邻格子的数取反.问:怎样操作使得所有格子都变为0.当有多组解时,优先取操作次数最小的:当操作次数相同时,优先取字典序最小的. 输入 第 ...

  6. BZOJ4869 六省联考2017相逢是问候(线段树+欧拉函数)

    由扩展欧拉定理,a^(a^(a^(……^x)))%p中x作为指数的模数应该是φ(φ(φ(φ(……p)))),而p取log次φ就会变为1,也即每个位置一旦被修改一定次数后就会变为定值.线段树维护区间剩余 ...

  7. 转 :hlda文献学习笔记

    David M.BLEI nCR文献学习笔记(基本完成了)  http://yhbys.blog.sohu.com/238343705.html 题目:The Nested Chinese Resta ...

  8. 【题解】JXOI2017颜色

    一眼线段树...显然,我们可以考虑最后所留下的区间,那显然这个区间中应当不能存在任何与区间外相同的颜色.这里的转化也是很常用的,我们用 \(nxt[i]\) 表示与 \(i\) 颜色相同的下一个位置在 ...

  9. [洛谷P4118][Ynoi2016]炸脖龙I([洛谷P3934]Nephren Ruq Insania)

    题目大意:有$n$个数,每个数为$s_i$,两个操作: $1\;l\;r\;x:$表示将区间$[l,r]$内的数加上$x$ $2\;l\;r\;p:$表示求$s_l^{s_{l+1}^{^{s_{l+ ...

  10. [GDOI2014]拯救莫莉斯 状压DP

    题面: 莫莉斯·乔是圣域里一个叱咤风云的人物,他凭借着自身超强的经济头脑,牢牢控制了圣域的石油市场. 圣域的地图可以看成是一个n*m的矩阵.每个整数坐标点(x , y)表示一座城市( 1\le x\l ...