2016ACM/ICPC亚洲区沈阳站(区域赛练习)

本文精选了五道算法竞赛题目,包括求汉堡最大厚度、计算分子相对原子质量、递归数列、计数完全子图及猜骰子游戏概率。通过详细解析与代码实现,帮助读者理解并掌握算法设计与实现技巧。

 

比赛链接:https://cn.vjudge.net/contest/257108

目录

A-Thickest Burger

B—Relative atomic mass

C—Recursive sequence

E-Counting Cliques

H-Guessing the Dice Roll


A-Thickest Burger

ACM ICPC is launching a thick burger. The thickness (or the height) of a piece of club steak is A (1 ≤ A ≤ 100). The thickness (or the height) of a piece of chicken steak is B (1 ≤ B ≤ 100). 
The chef allows to add just three pieces of meat into the burger and he does not allow to add three pieces of same type of meat. As a customer and a foodie, you want to know the maximum total thickness of a burger which you can get from the chef. Here we ignore the thickness of breads, vegetables and other seasonings.

Input

The first line is the number of test cases. For each test case, a line contains two positive integers A and B.

Output

For each test case, output a line containing the maximum total thickness of a burger.

Sample Input

10
68 42
1 35
25 70
59 79
65 63
46 6
28 82
92 62
43 96
37 28

Sample Output

178
71
165
217
193
98
192
246
235
102

Hint

Consider the first test case, since 68+68+42 is bigger than 68+42+42 the answer should be 68+68+42 = 178. 
Similarly since 1+35+35 is bigger than 1+1+35, the answer of the second test case should be 1+35+35 = 71.

 

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
	int a,b;
	int t;
	scanf("%d",&t);
    while(t--)
    {
    	int maxn;
    	scanf("%d%d",&a,&b);
    	if(a>b)
    	 maxn=a;
    	else
    	 maxn=b;
    	long long int sum=a+b+maxn;
    	printf("%lld\n",sum);
	}
	return 0;
}

B—Relative atomic mass

Relative atomic mass is a dimensionless physical quantity, the ratio of the average mass of atoms of an element (from a single given sample or source) to 1212of the mass of an atom of carbon-12 (known as the unified atomic mass unit). 
You need to calculate the relative atomic mass of a molecule, which consists of one or several atoms. In this problem, you only need to process molecules which contain hydrogen atoms, oxygen atoms, and carbon atoms. These three types of atom are written as ’H’,’O’ and ’C’ repectively. For your information, the relative atomic mass of one hydrogen atom is 1, and the relative atomic mass of one oxygen atom is 16 and the relative atomic mass of one carbon atom is 12. A molecule is demonstrated as a string, of which each letter is for an atom. For example, a molecule ’HOH’ contains two hydrogen atoms and one oxygen atom, therefore its relative atomic mass is 18 = 2 * 1 + 16. 
 

Input

The first line of input contains one integer N(N ≤ 10), the number of molecules. In the next N lines, the i-th line contains a string, describing the i-th molecule. The length of each string would not exceed 10.

Output

For each molecule, output its relative atomic mass.

Sample Input

5
H
C
O
HOH
CHHHCHHOH

Sample Output

1
12
16
18
46
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
char str[20];
int main()
{
   int t;
   scanf("%d",&t);
   getchar();
   while(t--)
   {
   	long long int sum=0;
   	  gets(str);
   	  int len= strlen(str);
   	  for(int i=0;i<len;i++)
   	  {
   	  	if(str[i]=='H')
   	  	{
   	  	    sum+=1;	
		 }
		 else if(str[i]=='O')
		 {
		 	sum+=16;
		 }
		 else
		 {
		 	sum+=12;
		 }
	  }
	  printf("%lld\n",sum);
   }
	return 0;
}

C—Recursive sequence

Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right. 

Input

The first line of input contains an integer t, the number of test cases. t test cases follow. 
Each case contains only one line with three numbers N, a and b where N,a,b < 231231 as described above. 

Output

For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.

Sample Input

2
3 1 2
4 1 10

Sample Output

85
369

Hint

In the first case, the third number is 85 = 2*1十2十3^4.
In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.
        

矩阵快速幂

 

/*
  // 错误代码  还是自己太天真
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long int ll;
ll num[100000];
ll POW(ll x)
{
	return x*x*x*x;
}
int main()
{
    ll t,n,a,b;
	scanf("%lld",&t);
	while(t--)
	{
		memset(num,0,sizeof(num));
	   scanf("%lld%lld%lld",&n,&num[1],&num[2]);
	   if(n==3)
	   {
	   	 ll sum=2*num[1]%2147493647+num[2]%2147493647+POW(3)%2147493647;
	   	printf("%lld\n",sum);
	   }
	   else
	   {
	   for(ll i=3;i<=n;i++)
	   {
	      num[i]=(2*num[i-2])%2147493647+(num[i-1]%2147493647)+(POW(i))%2147493647;
	   }	
	   printf("%lld\n",num[n]);	   	
	   }
    }
	return 0; 	 
} 
*/

 

 

E-Counting Cliques

A clique is a complete graph, in which there is an edge between every pair of the vertices. Given a graph with N vertices and M edges, your task is to count the number of cliques with a specific size S in the graph. 

Input

The first line is the number of test cases. For each test case, the first line contains 3 integers N,M and S (N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10), each of the following M lines contains 2 integers u and v (1 ≤ u < v ≤ N), which means there is an edge between vertices u and v. It is guaranteed that the maximum degree of the vertices is no larger than 20.

Output

For each test case, output the number of cliques with size S in the graph.

Sample Input

3
4 3 2
1 2
2 3
3 4
5 9 3
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
6 15 4
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6

Sample Output

3
7
15

剪枝+DFS

 

 

 

H-Guessing the Dice Roll

 

There are N players playing a guessing game. Each player guesses a sequence consists of {1,2,3,4,5,6} with length L, then a dice will be rolled again and again and the roll out sequence will be recorded. The player whose guessing sequence first matches the last L rolls of the dice wins the game. 

Input

The first line is the number of test cases. For each test case, the first line contains 2 integers N (1 ≤ N ≤ 10) and L (1 ≤ L ≤ 10). Each of the following N lines contains a guessing sequence with length L. It is guaranteed that the guessing sequences are consist of {1,2,3,4,5,6} and all the guessing sequences are distinct.

Output

For each test case, output a line containing the winning probability of each player with the precision of 6 digits.

Sample Input

3
5 1
1
2
3
4
5
6 2
1 1
2 1
3 1
4 1
5 1
6 1
4 3
1 2 3
2 3 4
3 4 5
4 5 6

Sample Output

0.200000 0.200000 0.200000 0.200000
0.200000
0.027778 0.194444 0.194444 0.194444
0.194444 0.194444
0.285337 0.237781 0.237781 0.239102

AC自动机+高斯消元+概率DP

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值