2021-01-20

灵动ICPC冬令营基础-3

A - Birthday Cake

Lucy and Lily are twins. Today is their birthday.
Mother buys a birthday cake for them. Now we put
the cake onto a Descartes coordinate. Its center is at
(0, 0), and the cake’s length of radius is 100.
There are 2N (N is a integer, 1 ≤ N ≤ 50) cherries
on the cake. Mother wants to cut the cake into two
halves with a knife (of course a beeline). The twins
would like to be treated fairly, that means, the shape
of the two halves must be the same (that means the
beeline must go through the center of the cake) , and
each half must have N cherrie(s). Can you help her?
Note: the coordinate of a cherry (x, y) are two integers. You must give the line as form two integers A,
B (stands for Ax + By = 0) each number mustn’t in
[−500, 500]. Cherries are not allowed lying on the beeline. For each dataset there is at least one solution.
Input
The input file contains several scenarios. Each of them consists of 2 parts:
The first part consists of a line with a number N, the second part consists of 2N lines, each line
has two number, meaning (x, y). There is only one space between two border numbers. The input file
is ended with N = 0.
Output
For each scenario, print a line containing two numbers A and B. There should be a space between
them. If there are many solutions, you can only print one of them.
Sample Input
2
-20 20
-30 20
-10 -50
10 -5
0
Sample Output
0 1

#include<cstdio>
int main(){
	int A,B,x[1000],y[1000],t;
	int n,up,down;
	int a,b;
	while(scanf("%d",&n)&&n){
		for(int i=1;i<=2*n;i++) scanf("%d%d",&x[i],&y[i]);
		for(A=-500;A<=500;A++){
			for(B=-500;B<=500;B++){
				up=down=0;t=0;
				if(A*B==0) continue;
				for(int i=1;i<=2*n;i++){
					if(x[i]>100||x[i]<-100||y[i]>100||y[i]<-100) continue;
					else if((A*x[i]+B*y[i])>0) up++;
				    else if((A*x[i]+B*y[i])<0) down++;
				    else if((A*x[i]+B*y[i])==0) break;
				}
				if(up==down&&up+down==2*n){
					a=A,b=B;
					t=1;
					break;
				}
			}
			if(t==1) break;
		}
		printf("%d %d\n",a,b);
	}
	return 0;
} 

分线的两边计草莓个数,排除不合理的情况。出现一种就退出。

B - Is This Integration ?

In the image below you can see a square
ABCD, where AB = BC = CD = DA =
a. Four arcs are drawn taking the four
vertexes A, B, C, D as centers and a as
the radius. The arc that is drawn taking A as center, starts at neighboring vertex B and ends at neighboring vertex D.
All other arcs are drawn in a similar fashion. Regions of three different shapes are
created in this fashion. You will have to
determine the total area if these different
shaped regions.
Input
The input file contains a floating-point
number a (0 ≤ a ≤ 10000) in each line
which indicates the length of one side of
the square. Input is terminated by end of
file.
Output
For each line of input, output in a single line the total area of the three types of region (filled with
different patterns in the image above).
These three numbers will of course be floating point numbers with three digits after the decimal
point. First number will denote the area of the striped region, the second number will denote the total
area of the dotted regions and the third number will denote the area of the rest of the regions.
Sample Input
0.1
0.2
0.3
Sample Output
0.003 0.005 0.002
0.013 0.020 0.007
0.028 0.046 0.016

#include<cstdio>
#include<cmath>
#define P 3.1415926535898
int main(){
    double a;
    while(scanf("%lf",&a)!=EOF){
    	double x,y,z;
    	x=(1-sqrt(3)+(P/3))*a*a;
    	y=((-1+(sqrt(3)/2))+(P/12))*a*a;
    	z=a*a-(sqrt(3)/4)*a*a-(P/6)*a*a;
    	printf("%.3lf %.3lf %.3lf\n",x,y*4,z*4);
	}
	return 0;
} 

画图做辅助线,列出面积与各个区域的关系表达式,最后求出公式

C - Simple division

Integer division between a dividend n and a divisor d yields a quotient q and a remainder r. q is the integer which maximizes q ∗ d
such that q ∗ d ≤ n and r = n − q ∗ d.
For any set of integers there is an integer d such that each of the
given integers when divided by d leaves the same remainder.
Input
Each line of input contains a sequence of nonzero integer numbers
separated by a space. The last number on each line is 0 and this
number does not belong to the sequence. There will be at least 2 and
no more than 1000 numbers in a sequence; not all numbers occuring
in a sequence are equal. The last line of input contains a single 0
and this line should not be processed.
Output
For each line of input, output the largest integer which when divided into each of the input integers
leaves the same remainder.
Sample Input
701 1059 1417 2312 0
14 23 17 32 122 0
14 -22 17 -31 -124 0
0
Sample Output
179
3
3

#include<cstdio>
int gcd(int a, int b){
	if(b==0) return a ;
	return gcd(b, a % b) ;
}
int abs(int x){
	return x>0?x:-x;
}
int main()
{
	int a[1005];
	int s, s1, i, n, g;
	while (scanf("%d", &s)&&s)
	{
		for (i = 0; scanf("%d", &s1)&&s1; ++i)
			a[i] = s1 - s, s = s1;
		n = i;
		g = a[0];
		for (i=1; i < n; ++i)
			g=gcd(a[i]==0?g:a[i],g);
		printf("%d\n", abs(g));
	}
	return 0;
}

套用欧几里得

D - Euclid Proble

From Euclid it is known that for any positive integers A and B there exist such integers X and Y that
AX + BY = D, where D is the greatest common divisor of A and B. The problem is to find for given
A and B corresponding X, Y and D.
Input
The input will consist of a set of lines with the integer numbers A and B, separated with space
(A, B < 1000000001).
Output
For each input line the output line should consist of three integers X, Y and D, separated with space.
If there are several such X and Y , you should output that pair for which |X| + |Y | is the minimal. If
there are several X and Y satisfying the minimal criteria, output the pair for which X ≤ Y .
Sample Input
4 6
17 17
Sample Output
-1 1 2
0 1 17

#include<cstdio> 
int exgcd(int a, int b, int &x, int &y){
	if (b==0) {x=1; y=0; return a;}
	int t=exgcd(b, a%b, x, y);
	int x0=x, y0=y;
	x=y0; y=x0-(a/b)*y0;
	return t;
}
int main(){
	int a,b,d,x,y;
	while(scanf("%d%d",&a,&b)!=EOF){
		d=exgcd(a,b,x,y);
		printf("%d %d %d\n",x,y,d);
	}
	return 0;
}

套用拓展欧几里得

E - Dead Fraction

Mike is frantically scrambling to finish his thesis at the last minute. He needs to assemble all his research notes into vaguely coherent form in the next 3 days. Unfortunately, he notices that he had been extremely sloppy in his calculations. Whenever he needed to perform arithmetic, he just plugged it into a calculator and scribbled down as much of the answer as he felt was relevant. Whenever a repeating fraction was displayed, Mike simply reccorded the first few digits followed by “…”. For instance, instead of “1/3” he might have written down “0.3333…”. Unfortunately, his results require exact fractions! He doesn’t have time to redo every calculation, so he needs you to write a program (and FAST!) to automatically deduce the original fractions.
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).
Input
There are several test cases. For each test case there is one line of input of the form “0.dddd…” where dddd is a string of 1 to 9 digits, not all zero. A line containing 0 follows the last case.
Output
For each case, output the original fraction.
Sample Input
0.2…
0.20…
0.474612399…
0
Sample Output
2/9
1/5
1186531/2500000
Hint
Note that an exact decimal fraction has two repeating expansions (e.g. 1/5 = 0.2000… = 0.19999…).

题目的分数计算方法懂了,但分循环节点代码实现还是有点困难

F - What is the Probability ?

Probability has always been an integrated part of computer algorithms. Where the deterministic
algorithms have failed to solve a problem in short time, probabilistic algorithms have come to the
rescue. In this problem we are not dealing with any probabilistic algorithm. We will just try to
determine the winning probability of a certain player.
A game is played by throwing a dice like thing (it should not be assumed that it has six sides like
an ordinary dice). If a certain event occurs when a player throws the dice (such as getting a 3, getting
green side on top or whatever) he is declared the winner. There can be N such player. So the first
player will throw the dice, then the second and at last the N-th player and again the first player and
so on. When a player gets the desired event he or she is declared winner and playing stops. You will
have to determine the winning probability of one (The I-th) of these players.
Input
Input will contain an integer S (S ≤ 1000) at first, which indicates how many sets of inputs are there.
The next S lines will contain S sets of inputs. Each line contain an integer N (N ≤ 1000) which denotes
the number players, a floating point number p which indicates the probability of the happening of a
successful event in a single throw (If success means getting 3 then p is the probability of getting 3 in
a single throw. For a normal dice the probability of getting 3 is 1/6), and I (I ≤ N) the serial of the
player whose winning probability is to be determined (Serial no varies from 1 to N). You can assume
that no invalid probability § value will be given as input.
Output
For each set of input, output in a single line the probability of the I-th player to win. The output
floating point number will always have four digits after the decimal point as shown in the sample output.
Sample Input
2
2 0.166666 1
2 0.166666 2
Sample Output
0.5455
0.4545

#include<cstdio>
int main(){
	int s,n,i;
	double p;
	scanf("%d",&s);
	while(s--){
		scanf("%d%lf%d",&n,&p,&i);
		double q=1-p;
		double tmp=q;
		for(int j=1;j<n;j++){
			tmp*=q;
		}
		double a=1;
		for(int j=1;j<i;j++){
			a*=q;
		}
		printf("%.4lf\n",p==0?0:p*a/(1-tmp));
	}
	return 0;
}

推导公式,用q=1-p来计算没中的概率,分别tmp和a记录p和q的n次方;

G - Burger

When Mr. and Mrs. Clinton’s twin sons Ben and Bill had their tenth birthday, the party was held
at the McDonald’s restaurant at South Broadway 202, New York. There were 20 kids at the party,
including Ben and Bill. Ronald McDonald had made 10 hamburgers and 10 cheeseburgers and when he
served the kids he started with the girl directly sitting left of Bill. Ben was sitting to the right of Bill.
Ronald flipped a (fair) coin to decide if the girl should have a hamburger or a cheeseburger, head for
hamburger, tail for cheeseburger. He repeated this procedure with all the other 17 kids before serving
Ben and Bill last. Though, when coming to Ben he didn’t have to flip the coin anymore because there
were no cheeseburgers left, only 2 hamburgers.
Ronald McDonald was quite surprised this happened, so he would like to know what the probability
is of this kind of events. Calculate the probability that Ben and Bill will get the same type of burger
using the procedure described above. Ronald McDonald always grills the same number of hamburgers
and cheeseburgers.
Input
The first line of the input-file contains the number of problems n , followed by n times:
a line with an even number [2,4,6,…,100000], which indicates the number of guests present at the party
including Ben and Bill.
Output
The output consists of n lines with on each line the probability (4 decimals precise) that Ben and Bill
get the same type of burger.
Note: a variance of ±0.0001 is allowed in the output due to rounding differences.
Sample Input
3
6
10
256
Sample Output
0.6250
0.7266
0.9500

#include<cstdio>
const int N=5e4+7;
double p[N];
int main(){
    int s,n;
	p[1] = 1; 
    for(int i = 1; i < 5e4; ++i) 
    p[i+1] = (2*i-1)*p[i]/(2*i);
    scanf("%d",&s);
    while(s--){
    	scanf("%d",&n);n=n/2;
    	printf("%.4lf\n",1-p[n]);
	}
	return 0;
}

算出每一个人吃汉堡的概率,除了最后两人,有2i-2个汉堡,选i-1个,有1/2的概率。Ci-12i-2 /1/22i-2.

H - Coin Toss

In a popular carnival game, a coin is tossed onto a table with an area that is covered with square tiles in a grid. The prizes are determined by the number of tiles covered by the coin when it comes to rest: the more tiles it covers, the better the prize. In the following diagram, the results from five coin tosses are shown:

In this example:

coin 1 covers 1 tile
coin 2 covers 2 tiles
coin 3 covers 3 tiles
coin 4 covers 4 tiles
coin 5 covers 2 tiles
Notice that it is acceptable for a coin to land on the boundary of the playing area (coin 5). In order for a coin to cover a tile, the coin must cover up a positive area of the tile. In other words, it is not enough to simply touch the boundary of the tile. The center of the coin may be at any point of the playing area with uniform probability. You may assume that (1) the coin always comes to a rest lying flat, and (2) the player is good enough to guarantee that the center of the coin will always come to rest on the playing area (or the boundary).

The probability of a coin covering a certain number of tiles depends on the tile and coin sizes, as well as the number of rows and columns of tiles in the playing area. In this problem, you will be required to write a program which computes the probabilities of a coin covering a certain number of tiles.

Input
The first line of input is an integer specifying the number of cases to follow. For each case, you will be given 4 integers m, n, t, and c on a single line, separated by spaces. The playing area consists of m rows and n columns of tiles, each having side length t. The diameter of the coin used is c. You may assume that 1 <= m, n <= 5000, and 1 <= c < t <= 1000.
Output
For each case, print the case number on its own line. This is followed by the probability of a coin covering 1 tile, 2 tiles, 3 tiles, and 4 tiles each on its own line. The probability should be expressed as a percentage rounded to 4 decimal places. Use the format as specified in the sample output. You should use double-precision floating-point numbers to perform the calculations. “Negative zeros” should be printed without the negative sign.

Separate the output of consecutive cases by a blank line.
Sample Input
3
5 5 10 3
7 4 25 20
10 10 10 4
Sample Output
Case 1:
Probability of covering 1 tile = 57.7600%
Probability of covering 2 tiles = 36.4800%
Probability of covering 3 tiles = 1.2361%
Probability of covering 4 tiles = 4.5239%

Case 2:
Probability of covering 1 tile = 12.5714%
Probability of covering 2 tiles = 46.2857%
Probability of covering 3 tiles = 8.8293%
Probability of covering 4 tiles = 32.3135%

Case 3:
Probability of covering 1 tile = 40.9600%
Probability of covering 2 tiles = 46.0800%
Probability of covering 3 tiles = 2.7812%
Probability of covering 4 tiles = 10.1788%

#include<cstdio>
#include<cmath>
const double PI=acos(-1.0);
int main(){
	double m,n,t,c;
	int s;
	scanf("%d",&s);
	for(int cas=1;cas<=s;cas++){
		scanf("%lf%lf%lf%lf",&m,&n,&t,&c);
		double sum=n*m*t*t;
		double s2=c * (t - c) * (2 * m * n - n - m)+c*(c/2)*(2*n+2*m-4);
		double s4=PI*c*c/4.0*(m-1)*(n-1);
		double s3=(m-1)*(n-1)*c*c-s4;
		double s1=sum-s2-s3-s4;
		printf("Case %d:\n",cas);
		printf("Probability of covering 1 tile  = %.4lf%%\n",s1/sum*100);
		printf("Probability of covering 2 tiles = %.4lf%%\n",s2/sum*100);
		printf("Probability of covering 3 tiles = %.4lf%%\n",s3/sum*100);
		printf("Probability of covering 4 tiles = %.4lf%%\n",s4/sum*100);
		if(cas!=s) puts("");
	}
	return 0;
}

把整个圆看成圆心在图形里运动,根据正方形的边长和圆的半径,用圆心的位置,判断覆盖了几条。覆盖4条是内部的线以半径为圆的面积,覆盖3条是内部面积减去4条的面积,覆盖俩条分内部和边上,一条用总面积减其他面积。

I - 498-bis

Looking throw the “Online Judge’s Problem Set Archive” I found a very interesting problem number
498, titled “Polly the Polynomial”. Frankly speaking, I did not solve it, but I derived from it this
problem.
Everything in this problem is a derivative of something from 498. In particular, 498 was “… designed
to help you remember … basic algebra skills, make the world a better place, etc., etc.”. This problem
is designed to help you remember basic derivation algebra skills, increase the speed in which world
becomes a better place, etc., etc.
In 498 you had to evaluate the values of polynomial
a0x
n + a1x
n−1 + . . . + an−1x + an.
In this problem you should evaluate its derivative. Remember that derivative is defined as
a0nxn−1 + a1(n − 1)x
n−2 + . . . + an−1.
All the input and output data will fit into integer, i.e. its absolute value will be less than 231
.
Input
Your program should accept an even number of lines of text. Each pair of lines will represent one
problem. The first line will contain one integer - a value for x. The second line will contain a list of
integers a0, a1, …, an−1, an, which represent a set of polynomial coefficients.
Input is terminated by ¡EOF¿.
Output
For each pair of lines, your program should evaluate the derivative of polynomial for the given value x
and output it in a single line.
Sample Input
7
1 -1
2
1 1 1
Sample Output
1
5

#include<cstdio>
int main(){
	int x;
	int ans,sum;
	while(~scanf("%d",&x)){
		ans=sum=0;
		int a;
		char c;
		while(~scanf("%d",&a)){
			ans=ans*x+sum;
			sum=sum*x+a;
			scanf("%c",&c);
			if(c=='\n') break;
		}
		printf("%d\n",ans);
	}
	return 0;
}

输入一个x,然后输入一个a处理一个
用学长推导的公式迭代。
求导式子的求和推导 多了系数 推导不容易。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值