Codeforces Round#411 Div.2

A. Fake NP

题面

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.

题意

求从l到r,公因素最多的数字是什么

hack点:l==r

代码

#include <bits/stdc++.h>
using namespace std;

int ans;

int main() 
{
	int l,r;
	cin>>l>>r;
	if (l==r) cout<<l; else cout<<2;
}

B. 3-palindrome

题面

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.

题意

造一个长度为n的字符串。使得长度为3的连续子串不是回文串

aabb循环就好了

代码

#include <bits/stdc++.h>
using namespace std;

int n; 
//char a[200010];

int main() 
{
	cin>>n;
	for (int i=1;i<=n;i++)
	if (i%4==1) cout<<"a";
	else if (i%4==2) cout<<"a";
	else if (i%4==3) cout<<"b";
	else if (i%4==0) cout<<"b";
}

C - Closed Rooms

题面

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 (i+j)%(n+1) 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.

题意

从1到n的学校,路费从i到j,cost(i+j)%(n+1)。问浏览完所有学校最小花费

浏览路线为:1,n,2,n-1,3,n-2,……

代码

#include <bits/stdc++.h>
using namespace std;

int n; 

int main() 
{
	cin>>n;
	cout<<(n-1)/2<<endl;
}

D. Minimum number of steps

题面

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.

题意

这题最后居然写挂了,原因是我快速幂用的int,结论题,找一下规律就好了,O(n)。心累不多说

代码

#include <bits/stdc++.h>
using namespace std;

string s; 
pair<long long,long long> a[1000010];
int x,y;
int cnt;
long long ans;
const int MOD=1000000007;

long long power(long long q,long long w)
{
    long long ret=1;
    q%=MOD;
    while (w)
    {
        if (w&1) ret=ret*q%MOD;
        w>>=1;
        q=q*q%MOD;
    }
    return ret%MOD;
}

int main() 
{
	cin>>s;
	int i=s.size()-1;
	while (i>=0)
	{
		while (i>=0 && s[i]=='a') i--;
		x=0;
		while (i>=0 && s[i]=='b') --i,++y;
		while (i>=0 && s[i]=='a') --i,++x;
		//a[cnt++]=make_pair(x,y);
		if (x) 
		{
			ans=(ans+((power(2,x)-1)*y)%MOD)%MOD;
			y=(power(2,x)*y)%MOD;
		}
	}
	//for (int i=0;i<cnt;i++) cout<<a[i].first<<" "<<a[i].second<<endl;
	
/*	while (cnt)
	{
		for (int i=0;i<cnt;i++) ans=(ans+((power(2,a[i].first)-1)*a[i].second)%MOD)%MOD;
		for (int i=0;i<cnt;i++) 
		{
			pair<long long,long long> now=a[i];
			a[i].first=(now.second*power(2,now.first))%MOD;
			a[i].second=now.first;
		}
		for (int i=0;i<cnt-1;i++) 
		{
			a[i].first=a[i].second;
			a[i].second=a[i+1].first;
		}
		cnt--;
	}*/
	
	cout<<ans;
	
}

E. Ice cream coloring

题面

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 ≤ m, u ≠ 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.

题意

给同一个点上的冰淇淋染色,有相同冰淇淋的点必定相邻。

因为有相同冰淇淋的点相邻,可以直接染色,不可能出现染色矛盾。

代码注意下 不能用memset,太大了会超时。

代码

#include <bits/stdc++.h>
using namespace std;

int n,m;
int maxi=1; 
int s,x;
vector<int> a[300010];
int col[300010];
int vis[300010];

vector<int> r[300010];

void dfs(int u,int fa)
{
	for (int o=0;o<=a[u].size();o++) vis[o]=0;
	for (int o=0;o<a[u].size();o++) if (col[a[u][o]]) vis[col[a[u][o]]]=1;
	int cnt=1;
	for (int o=0;o<a[u].size();o++) if (!col[a[u][o]])
	{
		int t=a[u][o];
		while (vis[cnt]) cnt++;			
		col[t]=cnt++;
	}
	
	for (int i=0;i<r[u].size();i++)
		if (r[u][i]!=fa) dfs(r[u][i],u);
}

int main() 
{
	cin>>n>>m;
	for (int i=1;i<=n;i++)
	{
		cin>>s;
		maxi=max(maxi,s);
		for (int o=0;o<s;o++) 
		{
			cin>>x;
			a[i].push_back(x);
		}
	}
	for (int i=1;i<n;i++)
	{
		cin>>s>>x;
		r[s].push_back(x);
		r[x].push_back(s);
	}
	dfs(1,-1);
	cout<<maxi<<endl;
	for (int i=1;i<=m;i++) if (col[i]) cout<<col[i]<<" ";else cout<<1<<" ";
}

F. Expected diameter of a tree

题面

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.

题意

给一个森林。给若干对u,v

若v,u同树,输出-1

否则输出两树相并的直径的期望。

我不会。

赛后总结

本来是四题的,但是第四题数据类型的原因搞错,最后FST。

第五题的话,事实上是没时间的,应该很好看出。但是我没注意到memset太大了,最后相当于N^2有点zz。。

比赛链接

http://codeforces.com/contest/805

原文地址:https://www.cnblogs.com/EDGsheryl/p/6854750.html