ACM基础题(共23道)

题目目录:

  1. CodeForces - 486A
  2. CodeForces 208A
  3. CodeForces 451A
  4. CodeForces 472A
  5. CodeForces 144A
  6. CodeForces 82A
  7. CodeForces 469A
  8. CodeForces 460A
  9. CodeForces 318A
  10. CodeForces 379A
  11. HDU 1576
  12. CodeForces 617A
  13. CodeForces 61A
  14. CodeForces 344A
  15. CodeForces 268A
  16. CodeForces 119A
  17. CodeForces 500A
  18. CodeForces 443A
  19. HDU 2191
  20. CodeForces 510A
  21. CodeForces 268B
  22. CodeForces 509A
  23. CodeForces 432A

1. CodeForces - 486A

A. Calculating Function

For a positive integer n let’s define a function f:
f(n) =  - 1 + 2 - 3 + … + ( - 1)nn
Your task is to calculate f(n) for a given integer n.

Input
The single line contains the positive integer n (1 ≤ n ≤ 1015).
Output
Print f(n) in a single line.

Examples
Input
4
Output
2
Input
5
Output
-3

Note
f(4) =  - 1 + 2 - 3 + 4 = 2
f(5) =  - 1 + 2 - 3 + 4 - 5 =  - 3

问题链接:http://codeforces.com/problemset/problem/486/A
问题简述:输入n,计算f(n) =  - 1 + 2 - 3 + … + ( - 1)^n*n
问题分析:水题,可使用列表法用数组存放结果,获取n后通过数组直接输出。或者分n为正数与负数两种情况讨论。
AC通过代码:

#include 
typedef long long LL;

int main()
{
	LL n,sum=0;
	scanf("%lld",&n);
	if(n%2==0)
	{
		sum=n/2;
	}else
	{
		sum=(n-1)/2-n;
	}
	printf("%lld",sum);
	return 0;
}

2. CodeForces 208A

A. Dubstep

Vasya works as a DJ in the best Berland nightclub, and he often uses dubstep music in his performance. Recently, he has decided to take a couple of old songs and make dubstep remixes from them.
Let’s assume that a song consists of some number of words. To make the dubstep remix of this song, Vasya inserts a certain number of words “WUB” before the first word of the song (the number may be zero), after the last word (the number may be zero), and between words (at least one between any pair of neighbouring words), and then the boy glues together all the words, including “WUB”, in one string and plays the song at the club.
For example, a song with words “I AM X” can transform into a dubstep remix as “WUBWUBIWUBAMWUBWUBX” and cannot transform into “WUBWUBIAMWUBX”.
Recently, Petya has heard Vasya’s new dubstep track, but since he isn’t into modern music, he decided to find out what was the initial song that Vasya remixed. Help Petya restore the original song.

Input
The input consists of a single non-empty string, consisting only of uppercase English letters, the string’s length doesn’t exceed 200 characters. It is guaranteed that before Vasya remixed the song, no word contained substring “WUB” in it; Vasya didn’t change the word order. It is also guaranteed that initially the song had at least one word.
Output
Print the words of the initial song that Vasya used to make a dubsteb remix. Separate the words with a space.

Examples
Input
WUBWUBABCWUB
Output
ABC
Input
WUBWEWUBAREWUBWUBTHEWUBCHAMPIONSWUBMYWUBFRIENDWUB
Output
WE ARE THE CHAMPIONS MY FRIEND

Note
In the first sample: “WUBWUBABCWUB” = “WUB” + “WUB” + “ABC” + “WUB”. That means that the song originally consisted of a single word “ABC”, and all words “WUB” were added by Vasya.
In the second sample Vasya added a single word “WUB” between all neighbouring words, in the beginning and in the end, except for words “ARE” and “THE” — between them Vasya added two “WUB”.

问题链接:http://codeforces.com/problemset/problem/208/A
问题简述(+分析):除去一串字符串中的“WUB”即可,若该WUB在字符串开头则直接去除,若在字符串中间则更改为一个空格。
程序说明:利用了gets获取了一整行输入的字符串便于处理。利用flag变量来表示WUB是否在开头位置。

AC通过代码:

#include
#include

int main()
{
	char s[205];
	gets(s);
	int flag=0,i=0;
	while(i

3. CodeForces 451A

After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of n horizontal and m vertical sticks.
An intersection point is any point on the grid which is formed by the intersection of one horizontal stick and one vertical stick.
In the grid shown below, n = 3 and m = 3. There are n + m = 6 sticks in total (horizontal sticks are shown in red and vertical sticks are shown in green). There are n·m = 9 intersection points, numbered from 1 to 9.
The rules of the game are very simple. The players move in turns. Akshat won gold, so he makes the first move. During his/her move, a player must choose any remaining intersection point and remove from the grid all sticks which pass through this point. A player will lose the game if he/she cannot make a move (i.e. there are no intersection points remaining on the grid at his/her move).
Assume that both players play optimally. Who will win the game?

Input
The first line of input contains two space-separated integers, n and m (1 ≤ n, m ≤ 100).
Output
Print a single line containing “Akshat” or “Malvika” (without the quotes), depending on the winner of the game.

Examples
Input
2 2
Output
Malvika
Input
2 3
Output
Malvika
Input
3 3
Output
Akshat

Note
Explanation of the first sample:
The grid has four intersection points, numbered from 1 to 4.
If Akshat chooses intersection point 1, then he will remove two sticks (1 - 2 and 1 - 3). The resulting grid will look like this.
Now there is only one remaining intersection point (i.e. 4). Malvika must choose it and remove both remaining sticks. After her move the grid will be empty.
In the empty grid, Akshat cannot make any move, hence he will lose.
Since all 4 intersection points of the grid are equivalent, Akshat will lose no matter which one he picks.

问题链接:codeforces.com/problemset/problem/451/A
问题简述(+分析):结果由横向纵向两个方向中stick数量少的一方决定,若该值为奇数,则Akshat赢得胜利,若该值为偶数,获胜者是Malvika。
程序说明:自己写了个小小的比较大小的子函数,也可使用alorithm这个头文件。

AC通过程序:

#include

int min(int a,int b)
{
	if(a<=b) return a;
	else return b;
}

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	int t=min(n,m);
	if(t%2==0)
	{
		printf("Malvika");
	}else
	{
		printf("Akshat");
	}
	return 0;
}

4. CodeForces 472A

A. Design Tutorial: Learn from Math

One way to create a task is to learn from math. You can generate some random math statement or modify some theorems to get something new and build a new task from that.
For example, there is a statement called the “Goldbach’s conjecture”. It says: “each even number no less than four can be expressed as the sum of two primes”. Let’s modify it. How about a statement like that: “each integer no less than 12 can be expressed as the sum of two composite numbers.” Not like the Goldbach’s conjecture, I can prove this theorem.
You are given an integer n no less than 12, express it as a sum of two composite numbers.

Input
The only line contains an integer n (12 ≤ n ≤ 106).
Output
Output two composite integers x and y (1 < x, y < n) such that x + y = n. If there are multiple solutions, you can output any of them.

Examples
Input
12
Output
4 8
Input
15
Output
6 9
Input
23
Output
8 15
Input
1000000
Output
500000 500000

Note
In the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output “6 6” or “8 4” as well.
In the second example, 15 = 6 + 9. Note that you can’t output “1 14” because 1 is not a composite number.

问题链接:http://codeforces.com/problemset/problem/472/A
问题简述:就是强行拆这个数字,建议推导可得知必定会得到“4 xx”与“9 xx”两种形式满足所有条件,结果显而易见。
程序说明:略

AC通过代码:

#include

int main()
{
    int n;
	scanf("%d",&n);
    if(n%2==0)
	{
		printf("4 %d\n",n-4);
	}
    else printf("9 %d\n",n-9);
    return 0;
}

5. CodeForces 144A

A. Arrival of the General
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.
By the military charter the soldiers should stand in the order of non-increasing of their height. But as there’s virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.
For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.
Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.

Input
The first input line contains the only integer n (2 ≤ n ≤ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, …, an (1 ≤ ai ≤ 100) the values of the soldiers’ heights in the order of soldiers’ heights’ increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, …, an are not necessarily different.
Output
Print the only integer — the minimum number of seconds the colonel will need to form a line-up the general will like.

Examples
Input
4
33 44 11 22
Output
2
Input
7
10 10 58 31 63 40 76
Output
10

Note
In the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).
In the second sample the colonel may swap the soldiers in the following sequence:
1、(10, 10, 58, 31, 63, 40, 76)
2、(10, 58, 10, 31, 63, 40, 76)
3、(10, 58, 10, 31, 63, 76, 40)
4、(10, 58, 10, 31, 76, 63, 40)
5、(10, 58, 31, 10, 76, 63, 40)
6、(10, 58, 31, 76, 10, 63, 40)
7、(10, 58, 31, 76, 63, 10, 40)
8、(10, 58, 76, 31, 63, 10, 40)
9、(10, 76, 58, 31, 63, 10, 40)
10、(76, 10, 58, 31, 63, 10, 40)
11、(76, 10, 58, 31, 63, 40, 10)

问题链接:http://codeforces.com/problemset/problem/144/A
问题简述:把最大的移动到最前,把最小的移动到最后,一次只能移动一次,问最少移动几次?
问题分析:找出最大和最小,最大要往左移,最小要往右移,简单画图可知,若最大的数在最小的数左边,两个数字分别到首尾的距离之和即为所求值,而若最大值在最小值的右边,该值要多加1.
程序说明:emmmm无。

AC通过程序:

#include

int main()
{
	int n;
	scanf("%d",&n);
	int s[101];
	scanf("%d",&s[0]);
	int max=s[0],min=s[0];
	int min_num=0,max_num=0;
	for(int i=1;imax)
		{
			max=s[i];
			max_num=i;
		}
		if(s[i]<=min)
		{
			min=s[i];
			min_num=i;
		}
	}
	if(max_num

6. CodeForces 82A

A. Double Cola
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a “Double Cola” drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum.
For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny.
Write a program that will print the name of a man who will drink the n-th can.
Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon.

Input
The input data consist of a single integer n (1 ≤ n ≤ 109).
It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers.
Output
Print the single line — the name of the person who drinks the n-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: “Sheldon”, “Leonard”, “Penny”, “Rajesh”, “Howard” (without the quotes). In that order precisely the friends are in the queue initially.

Examples
Input
1
Output
Sheldon
Input
6
Output
Sheldon
Input
1802
Output
Penny

问题链接:http://codeforces.com/problemset/problem/82/A
问题简述:一开始五个不同的人,轮流排队和水,喝水的人分裂成两人,然后排到队尾。
问题分析:容易分析出等比数列5,10,20,40.。。。可借此解题。

AC通过代码:

#include
#include

typedef long L;

int main()
{
	int t;
	long n;
	scanf("%ld",&n);
	L s[29]={};
	s[0]=1;
	for(int i=1;i<29;i++)
	{
		s[i]=s[i-1]*2;
	}
	if(n>5)
	{
		int t1=n/5+1;
		for(int i=0;;i++)
		{
			if(t1

7. CodeForces 469A

There is a game called “I Wanna Be the Guy”, consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.
Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?

Input
The first line contains a single integer n (1 ≤  n ≤ 100).
The next line contains an integer p (0 ≤ p ≤ n) at first, then follows p distinct integers a1, a2, …, ap (1 ≤ ai ≤ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It’s assumed that levels are numbered from 1 to n.
Output
If they can pass all the levels, print “I become the guy.”. If it’s impossible, print “Oh, my keyboard!” (without the quotes).

Examples
Input
4
3 1 2 3
2 2 4
Output
I become the guy.
Input
4
3 1 2 3
2 2 3
Output
Oh, my keyboard!

Note
In the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.
In the second sample, no one can pass level 4.

问题链接:http://codeforces.com/problemset/problem/469/A
问题分析:判断两人的数据中是否有1~n中的每个数字。水题
程序说明:创建了一个布尔数组,初始化为false,暴力扫描每个人的每一个数组,并使该数值对应的布尔数组中对应的元素改为true,最后判断布尔数组中0~(n-1)是否均为true,若是则输出I become the guy.否则输出Oh, my keyboard!

AC通过代码:

#include

int main()
{
	int n;
	bool s[101]={};
	int a,b;
	scanf("%d",&n);
	for(int i=0;i<2;i++)
	{
		scanf("%d",&a);
		for(int j=0;j

8. CodeForces 460A

Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When he comes home in the evening, Vasya takes off the used socks and throws them away. Every m-th day (at days with numbers m, 2m, 3m, …) mom buys a pair of socks to Vasya. She does it late in the evening, so that Vasya cannot put on a new pair of socks before the next day. How many consecutive days pass until Vasya runs out of socks?

Input
The single line contains two integers n and m (1 ≤ n ≤ 100; 2 ≤ m ≤ 100), separated by a space.
Output
Print a single integer — the answer to the problem.

Examples
Input
2 2
Output
3
Input
9 3
Output
13

Note
In the first sample Vasya spends the first two days wearing the socks that he had initially. Then on day three he puts on the socks that were bought on day two.
In the second sample Vasya spends the first nine days wearing the socks that he had initially. Then he spends three days wearing the socks that were bought on the third, sixth and ninth days. Than he spends another day wearing the socks that were bought on the twelfth day.

问题链接:http://codeforces.com/problemset/problem/460/A
问题简述:每天穿一双袜子,不可重复穿,原先有n双袜子,没m天增多一双,问总共能穿几天?
程序说明:n-m+1所在的循环用来计算增多的袜子的数量。

AC:

#include
int main()
{
	int n,m,d;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		d=n;
		while(n>=m)
	    {
	    	n=n-m+1;
	    	d++;
	    }
		printf("%d\n",d);
	}
	return 0;
}

9. CodeForces 318A

Being a nonconformist, Volodya is displeased with the current state of things, particularly with the order of natural numbers (natural number is positive integer number). He is determined to rearrange them. But there are too many natural numbers, so Volodya decided to start with the first n. He writes down the following sequence of numbers: firstly all odd integers from 1 to n (in ascending order), then all even integers from 1 to n (also in ascending order). Help our hero to find out which number will stand at the position number k.

Input
The only line of input contains integers n and k (1 ≤ k ≤ n ≤ 1012).
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
Print the number that will stand at the position number k after Volodya’s manipulations.

Examples
Input
10 3
Output
5
Input
7 7
Output
6

Note
In the first sample Volodya’s sequence will look like this: {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}. The third place in the sequence is therefore occupied by the number 5.

问题链接:http://codeforces.com/problemset/problem/318/A
问题简述:1~n个数字,按奇数到偶数,从大到小的顺序排列,求第k个是什么?水题,分析略
程序说明:注意输入数据很大。

AC:

#include
using namespace std;

int main()
{
	__int64 n,k,t1,t2;
	while(scanf("%I64d%I64d",&n,&k)!=EOF)
	{
		if(n>=2*k-1)
		{
			printf("%I64d\n",2*k-1);
		}else
		{
			t1=(n+1)/2;
			t2=k-t1;
			printf("%I64d\n",2*t2);
		}
	}
	return 0;
}

10. CodeForces 379A

Vasily the Programmer loves romance, so this year he decided to illuminate his room with candles.
Vasily has a candles.When Vasily lights up a new candle, it first burns for an hour and then it goes out. Vasily is smart, so he can make b went out candles into a new candle. As a result, this new candle can be used like any other new candle.
Now Vasily wonders: for how many hours can his candles light up the room if he acts optimally well? Help him find this number.

Input
The single line contains two integers, a and b (1 ≤ a ≤ 1000; 2 ≤ b ≤ 1000).
Output
Print a single integer — the number of hours Vasily can light up the room for.

Examples
Input
4 2
Output
7
Input
6 3
Output
8

问题链接:http://codeforces.com/problemset/problem/379/A
问题描述:同8
问题分析:和第八题本质完全一致,代码都用同一个。

AC:

#include
int main()
{
	int n,m,d;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		d=n;
		while(n>=m)
	    {
	    	n=n-m+1;
	    	d++;
	    }
		printf("%d\n",d);
	}
	return 0;
}

11. HDU 1576

要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。

Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
对应每组数据输出(A/B)%9973。

Sample Input
2
1000 53
87 123456789
Sample Output
7922
6060

问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576
问题分析:逆元法可轻易求解。

AC:

#include
int main()
{
	int t;
	scanf("%d",&t);
	long long b;
	int n;
	for(int i=0;i

12. CodeForces 617A

An elephant decided to visit his friend. It turned out that the elephant’s house is located at point 0 and his friend’s house is located at point x(x > 0) of the coordinate line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend’s house.

Input
The first line of the input contains an integer x (1 ≤ x ≤ 1 000 000) — The coordinate of the friend’s house.
Output
Print the minimum number of steps that elephant needs to make to get from point 0 to point x.

Examples
Input
5
Output
1
Input
12
Output
3

Note
In the first sample the elephant needs to make one step of length 5 to reach the point x
In the second sample the elephant can get to point x if he moves by 3, 5 and 4. There are other ways to get the optimal answer but the elephant cannot reach x in less than three moves.

问题链接:http://codeforces.com/problemset/problem/617/A
问题简述:大象从0走到x最少需要走几步。
问题分析:水题,让x除以5可轻易求解。

AC:

#include
using namespace std;
int main()
{
	int x;
	cin>>x;
	if(x%5==0)
	{
		cout<

13. CodeForces 61A

Shapur was an extremely gifted student. He was great at everything including Combinatorics, Algebra, Number Theory, Geometry, Calculus, etc. He was not only smart but extraordinarily fast! He could manage to sum 1018 numbers in a single second.
One day in 230 AD Shapur was trying to find out if any one can possibly do calculations faster than him. As a result he made a very great contest and asked every one to come and take part.
In his contest he gave the contestants many different pairs of numbers. Each number is made from digits 0 or 1. The contestants should write a new number corresponding to the given pair of numbers. The rule is simple: The i-th digit of the answer is 1 if and only if the i-th digit of the two given numbers differ. In the other case the i-th digit of the answer is 0.
Shapur made many numbers and first tried his own speed. He saw that he can perform these operations on numbers of length ∞ (length of a number is number of digits in it) in a glance! He always gives correct answers so he expects the contestants to give correct answers, too. He is a good fellow so he won’t give anyone very big numbers and he always gives one person numbers of same length.
Now you are going to take part in Shapur’s contest. See if you are faster and more accurate.

Input
There are two lines in each input. Each of them contains a single number. It is guaranteed that the numbers are made from 0 and 1 only and that their length is same. The numbers may start with 0. The length of each number doesn’t exceed 100.
Output
Write one line — the corresponding answer. Do not omit the leading 0s.

Examples
Input
1010100
0100101
Output
1110001
Input
000
111
Output
111
Input
1110
1010
Output
0100
Input
01110
01100
Output
00010

问题链接:http://codeforces.com/problemset/problem/61/A
问题简述:输入两行长度相同的只含0 1的字符串,若这两行相同位置的数字相同则输出0,不同输出1。水~
程序说明:扫描,对比。

AC:

#include
#include
int main()
{
	char A[101]={},B[101]={};
	int a=0,b=0;
	while(1)
	{
		char x1=getchar();
		if(x1=='\n') break;
		A[a++]=x1;
	}
	while(1)
	{
		char x2=getchar();
		if(x2=='\n') break;
		B[b++]=x2;
	}
	for(int i=0;i

14. CodeForces 344A

Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn’t need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a “plus”) and negative (a “minus”). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.
Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own.
Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.

Input
The first line of the input contains an integer n (1 ≤ n ≤ 100000) — the number of magnets. Then n lines follow. The i-th line (1 ≤ i ≤ n) contains either characters “01”, if Mike put the i-th magnet in the “plus-minus” position, or characters “10”, if Mike put the magnet in the “minus-plus” position.
Output
On the single line of the output print the number of groups of magnets.
Examples

Input
6
10
10
10
01
10
10
Output
3
Input
4
01
01
10
10
Output
2

Note
The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.
The second testcase has two groups, each consisting of two magnets.

问题链接:http://codeforces.com/problemset/problem/344/A
问题分析:求连续的01和10组数的和。

AC:

#include
using namespace std;
int main()
{
	long n,count=1;
	int x,temp;
	cin>>n;
	cin>>temp;
	n--;
	while(n--)
	{
		cin>>x;
		if(x!=temp) count++;
		temp=x;
	}
	cout<

15. CodeForces 268A

Manao works on a sports TV. He’s spent much time watching the football games of some country. After a while he began to notice different patterns. For example, each team has two sets of uniforms: home uniform and guest uniform. When a team plays a game at home, the players put on the home uniform. When a team plays as a guest on somebody else’s stadium, the players put on the guest uniform. The only exception to that rule is: when the home uniform color of the host team matches the guests’ uniform, the host team puts on its guest uniform as well. For each team the color of the home and guest uniform is different.
There are n teams taking part in the national championship. The championship consists of n·(n - 1) games: each team invites each other team to its stadium. At this point Manao wondered: how many times during the championship is a host team going to put on the guest uniform? Note that the order of the games does not affect this number.
You know the colors of the home and guest uniform for each team. For simplicity, the colors are numbered by integers in such a way that no two distinct colors have the same number. Help Manao find the answer to his question.

Input
The first line contains an integer n (2 ≤ n ≤ 30). Each of the following n lines contains a pair of distinct space-separated integers hi, ai (1 ≤ hi, ai ≤ 100) — the colors of the i-th team’s home and guest uniforms, respectively.
Output
In a single line print the number of games where the host team is going to play in the guest uniform.

Examples
Input
3
1 2
2 4
3 4
Output
1
Input
4
100 42
42 100
5 42
100 5
Output
5
Input
2
1 2
1 2
Output
0

Note
In the first test case the championship consists of 6 games. The only game with the event in question is the game between teams 2 and 1 on the stadium of team 2.
In the second test sample the host team will have to wear guest uniform in the games between teams: 1 and 2, 2 and 1, 2 and 3, 3 and 4, 4 and 2 (the host team is written first).

问题链接:http://codeforces.com/problemset/problem/268/A
问题简述:有n个球队,每个球队分主场和客场,所有球队两两之间进行一场比赛,要求双方球服颜色不能相同,问你需要准备多少种球服。
问题分析:给你n对数,统计每一对数中后一个数和其他对数中第一个数相等的情况。两层for循环显然足够。

AC:

#include
using namespace std;
int main()
{
	int n,count=0;
	cin>>n;
	int h[101]={},a[101]={};
	for(int i=0;i>h[i]>>a[i];
	}
	for(int i=0;i

17. CodeForces 500A

New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.
So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1 positive integers a1, a2, …, an - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ ai ≤ n - i holds. Next, he made n - 1 portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + ai), and one can travel from cell i to cell (i + ai) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (i + ai) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ ai ≤ n - i one can’t leave the Line World using portals.
Currently, I am standing at cell 1, and I want to go to cell t. However, I don’t know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.

Input
The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.
The second line contains n - 1 space-separated integers a1, a2, …, an - 1 (1 ≤ ai ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.
Output
If I can go to cell t using the transportation system, print “YES”. Otherwise, print “NO”.

Examples
Input
8 4
1 2 1 2 1 2 1
Output
YES
Input
8 5
1 2 1 2 1 1 1
Output
NO

Note
In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.
In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can’t visit the cell 5, which we want to visit.

问题链接:http://codeforces.com/problemset/problem/500/A
AC:

#include
using namespace std;
int main()
{
	int n,t,s[30001]={};
	cin>>n>>t;
	int now=1;
	for(int i=1;i>s[i];
	}
	int t1=0;
	for(int i=0;now

18. CodeForces 443A

19. HDU 2191

20. CodeForces 510A

21. CodeForces 268B

22. CodeForces 509A

23. CodeForces 432A

你可能感兴趣的:(ACM周赛)