Problem E: Expired License

**

Problem E: Expired License(German Collegiate Programming Contest 2018)

**

Paul is an extremely gifted computer scientist who just completed his master’s degree at a
prestigious German university. Now he would like to culminate his academic career in a PhD.
The problem is that there are so many great universities out there that it is hard for him to
pick the best. Because some application deadlines are coming up soon, Paul’s only way to
procrastinate his decision is by simply applying to all of them.
Most applications require Paul to attach a portrait photo. However, it seems like there does
not exist an international standard for the aspect ratio of these kinds of photos. While most
European universities ask Paul to send a photograph with aspect ratio 4.5 by 6, some Asian
countries discard the applications immediately if the photo does not have an aspect ratio of 7.14
by 11.22, precisely.
As Paul has never been interested in photo editing, he never had a reason to spend a lot of money
on proper software. He downloaded a free trial version some months ago, but that version
has already expired and now only works with some funny restrictions. The cropping tool, for
example, no longer accepts arbitrary numbers for setting the aspect ratio, but only primes. This
makes Paul wonder whether the desired aspect ratios can even be properly expressed by two
prime numbers. Of course, in case this is possible, he would also like to know the primes he has
to enter.

Input
The input consists of:
• one line with an integer n (1 ≤ n ≤ 105
), the number of applications Paul has to file;
• n lines, each with two real numbers a and b (0 < a, b < 100), where a × b is the desired
aspect ratio of one application.
All real numbers are given with at most 5 decimal places after the decimal point.

Output
For each application, if it is possible to represent the desired aspect ratio by two prime numbers
p and q, output one line with p and q. Otherwise, output impossible. If multiple solutions
exist, output the one minimizing p + q.

Sample Input 1
3
4.5 6
7.14 11.22
0.00002 0.00007

Sample Output 1
impossible
7 11
2 7

题意

给你两个浮点数,问你这两个浮点数的比值能否用两个质数p,q的比值来代替。
这里题意最后说明了一下,如果有多种情况则可以用最小的p+q来表示。我们知道两个质数的约数只有他本身和1,不可能有其他情况了,那么怎么可能有多种情况呢?只有当p,q是相同的两个质数的时候才能出现这种情况,于是特判一下如果输入的两个浮点数相同时,我们直接可以输出2比2。(0和1不是质数)
精度问题用llong long和double好一些。

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=10000050;
bool isprime[maxn];
ll prime[maxn];
int cnt;

ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}

void init(){
	cnt=0;isprime[0]=isprime[1]=1;
	for(ll i=2;i<maxn;i++){
		if(!isprime[i])	prime[++cnt]=i;
		for(ll j=1;j<=cnt;j++){
			if(i*prime[j]>=maxn)	break;
			isprime[i*prime[j]]=1;
			if(i%prime[j]==0)	break;
		}
	}
}
void solve(){
	init();
	int T;
	scanf("%d",&T);
	while(T--){
		double a,b;
		scanf("%lf %lf",&a,&b);
		if(a == b) printf("2 2\n");
		else{
			ll a0=(ll)(a*100000.0+0.5),b0=(ll)(b*100000.0+0.5);
			ll GCD=gcd(a0,b0);
			a0/=GCD;b0/=GCD;
			if(isprime[a0]||isprime[b0])	printf("impossible\n");
			else	printf("%lld %lld\n",a0,b0);	
		}
	}
}
int main(){
	solve();
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值