2011阿里巴巴程序设计公开赛 / 1009 Board Game Dice

本文介绍了一个关于概率计算的问题,通过理解题目意思并推导出计算公式:mx*x/n。文章给出了具体的C++代码实现,并考虑了精度问题及输出格式的要求。

这题一看就是算概率,把题意弄懂后,顺利推出公式,x*1/(n/mx) = mx*x/n.

因为精度wa了一次= =,然后输出少了空格PE了一次,额...


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define int64 long long
int64 n, m, x;
#define INF 21623
int64 gcd(int64 a, int64 b)
{
	return b==0? a: gcd(b, a%b);
}

int64 getx(int64& mx)
{
	int64 ans = 1;
	int64 tn = n;	int64 tm = m;
	while(tm<tn)
	{
//printf("%d*%d=%d\n", tm, tm*tm);
		if((int64)tm*tm>tn)
		{
			tm*=m;
			ans+=1;
		}
		else if((int64)tm*tm<=tn)
		{
			tm*=tm;
			ans*=2;
		}
	}
	mx = tm;
	return ans;
}

void solve(int64& a, int64& b)
{
	int64 mx;
	x = getx(mx);
	int64 d = gcd((int64)mx*x, (int64)n);

	a = (int64)mx*x/d;
	b = n/d;
}

int main()
{
	int t;	cin>>t;
	for(int i=1; i<=t; i++)
	{
		cin>>n>>m;
		int64 a, b;
		solve(a,  b);
		cout<<"Case "<<i<<": "<<a<<"/"<<b<<endl;
	}
}

转载于:https://www.cnblogs.com/tclh123/archive/2011/08/18/2587081.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值