【POJ 3440】 Coin Toss(概率公式)

【POJ 3440】 Coin Toss(概率公式)

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 3591   Accepted: 957

Description

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:

【POJ 3440】 Coin Toss(概率公式)_第1张图片

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%


题目大意:有n*m块瓷砖,摆成一个大矩形。每块瓷砖是一个t*t的正方形

随意扔一个直径为d的圆盘,问能覆盖1/2/3/4个瓷砖的概率


其实就是问覆盖i个瓷砖情况的圆心可处在的面积和除上总面积

公式嘛……无脑推就好,注意下细节,然后就是加减乘除恶心点,还有记得都用double int*int可能会爆


代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;
const double PI = acos(-1.0);


int main()
{
	//fread();
	//fwrite();

	int t;
	double n,m,per,d,ans[4],sum;
	scanf("%d",&t);

	for(int z = 1; z <= t; ++z)
	{
		scanf("%lf%lf%lf%lf",&n,&m,&per,&d);

		if(z != 1) puts("");
		printf("Case %d:\n",z);
		sum = per*per*n*m;

		ans[0] = (per-d)*(per-d)*n*m+(per-d)*(d/2)*(n*2+m*2)+d*d;
		ans[0] /= sum;

		ans[1] = (per-d)*d*(m*(n-1)+n*(m-1))+d*d/2*((m-1)*2+(n-1)*2);
		ans[1] /= sum;

		ans[2] = (d*d-PI*d*d/4)*((m-1)*(n-1));
		ans[2] /= sum;

		ans[3] = PI*d*d/4*((m-1)*(n-1));
		ans[3] /= sum;

		printf("Probability of covering 1 tile  = %.4f%%\n",ans[0]*100);
		printf("Probability of covering 2 tiles = %.4f%%\n",ans[1]*100);
		printf("Probability of covering 3 tiles = %.4f%%\n",ans[2]*100);
		printf("Probability of covering 4 tiles = %.4f%%\n",ans[3]*100);
	}

	return 0;
}




你可能感兴趣的:(【POJ 3440】 Coin Toss(概率公式))