跳到正文

题解

题解:P10667 BZOJ2712 [Violet 2] 棒球

qinyiyang2012约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月13日
收录
2026年2月6日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P10667

版本

  1. v1当前

思路分析

设安打次数为 pp,打击次数为 qq,且 p<q,q≠0p < q,q \ne 0。 pq\frac{p}{q} 四舍五入后为 rr 则意味着 r−510n+1⩽pq<r+510n+1r - \frac{5}{10^{n+1}} \leqslant \frac{p}{q} < r + \frac{5}{10^{n+1}}。

例如安达率四舍五入后为 0.3160.316,则 0.3155⩽pq<0.31650.3155 \leqslant \frac{p}{q} < 0.3165。

注意到这其实是 P5179 Fraction,我们只需要再处理一下 r−510n+1=pqr - \frac{5}{10^{n+1}} = \frac{p}{q} 的特殊情况即可。

代码

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
string r;
ll qpow(ll x,ll y){
	ll res=1;
	while(y){
		if(y&1)res*=x;
		x*=x;
		y>>=1;
	}
	return res;
}
void query(ll a,ll b,ll& p,ll& q,ll c,ll d){
	if(a<b&&c>d){
		p=q=1;
		return;
	}
	query(d%c,c,q,p,b-d/c*a,a);
	q+=d/c*p;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	while(cin>>n>>r){
		ll x=0,p,q;
		for(int i=2;i<r.size();i++)x=x*10+(r[i]-'0');
		if(!x){//安达率为0记得特判
			cout<<"1\n";
			continue;
		}
		ll a=x*10-5,b=qpow(10,n+1),c=x*10+5,d=qpow(10,n+1);
    //处理取等号的情况
		ll p1=a,q1=b,g=__gcd(p1,q1);
		p1/=g,q1/=g;
		query(a,b,p,q,c,d);
		cout<<min(q,q1)<<"\n";
	}
	return 0;
}
原文
存档评论3 条抓取于