题目大意:
A Greeting from Qinhuangdao
给你a个红球,b个蓝球,从这些球中取出两个红球的概率是多少?
答案用不可约分数表示,概率为0则为0 / 1,概率为1则为1 / 1
思路:
先特判a < 2 为0 / 1 的情况和b = 0 为1 / 1 的情况
分子为a ∗ ( a − 1 )
分母为( a + b ) ∗ ( a + b − 1 )
算出他们的gcd除一下即可
AC Code:
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<math.h>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
// #define int long long
#define debug(a) cout<<#a<<"="<<a<<endl;
typedef long long ll;
const double PI=acos(-1.0);
const double e=exp(1.0);
const int M=1e9+7;
const int N=2e5+7;
inline int mymax(int x,int y){return x>y?x:y;}
inline int mymin(int x,int y){return x<y?x:y;}
inline int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int a, b, k;
int fz, fm;
void solve(){
cin>>a>>b;
printf("Case #%d: ", ++k);
if(a<2) {printf("0/1\n"); return ;}
if(b==0) {printf("1/1\n"); return ;}
fz=a*(a-1);
fm=(a+b)*(a+b-1);
int temp=gcd(fz,fm);
fz/=temp;
fm/=temp;
printf("%d/%d\n", fz, fm);
return ;
}
signed main(){
int T;
cin>>T;
while(T--) solve();
return 0;
}
Comments | NOTHING