codeforce round#411div2

Codeforces Round #411 (Div. 2)
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}.

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 3appearing 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

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

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 .

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".

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.

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 .

自己第一次打codeforce,就写了一个题。感觉题目考查的更多的是思维的灵活性,而不是一些固定算法的应用。

题解:

A:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
int main()
{	
	int l,r;
	while(cin>>l>>r){
		if(l==r)
			cout<<l<<endl;
		else
			cout<<"2"<<endl;
	}
	return 0;
} 



#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
int main()
{	
	int n;
	while(cin>>n){
		for(int i=1;i<=n;i++){
			if(i%4==1||i%4==2)
				cout<<"a";
			else
				cout<<"b";
		}
		cout<<endl;
	}
	return 0;
} 
这题只要不出现回文串就好了,随便找出一个类别,比如aabbaabb排列就好了。




C:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <climits>
using namespace std;
int main()
{	
	int n;
	while(cin>>n){
		int ans;
		if(n%2)
		 	ans=(n-1)/2;
		else
			ans=(n-2)/2;
		cout<<ans<<endl;
	} 
	return 0;
} 


D:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <climits>
using namespace std;
#define maxn 1000000007
#define limit 1000005
#define ull long long
char  inf[limit];
ull fast_pow(ull b,int p,ull k)  //p=3,a=2,b=4,p=1-->a=8,b=16,p=0-->循环暂停 
{  
    ull a=1;  
    while(p)  
    {  
        if(p&1) a=a*b%k;  
        b=b*b%k;  
        p>>=1;  
    }  
    return a;  
}   
int main()
{	
	
	while(cin>>inf){
		int len=strlen(inf);
//		int num_b[limit];
		
		int temp;
		int sum=0;
		int ans=0;
		for(int i=0;i<len;i++){
			temp=0;
			while(inf[i]=='a'&&i<len){
				temp++;
				i++;
			}
			if(i!=len){
				sum=(sum+temp)%maxn;
				ans=(ans+fast_pow(2,sum,maxn)-1)%maxn;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
} 
或:
#include <cstdio>
#include <cstring>
using namespace std;
const int N=1000050,p=1000000007;
char s[N];
int n,ans,res;
int main(){
	scanf("%s",s+1),n=strlen(s+1);
	for(int i=n;i;i--)
		if(s[i]=='b')res=(res+1)%p;
		else (ans+=res)%=p,(res+=res)%=p;
	printf("%d\n",ans);
}
即从前面开始找规律和从后面找规律。

EF不会了





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值