HDU5974--A Simple Math Problem &&CF 1076C--Meme Problem(解一元二次方程)

2 篇文章 0 订阅
1 篇文章 0 订阅

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5974                         

                                     A Simple Math Problem

                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                  Total Submission(s): 5635    Accepted Submission(s): 1755


 

Problem Description

Given two positive integers a and b,find suitable X and Y to meet the conditions:
                                                        X+Y=a
                                              Least Common Multiple (X, Y) =b

 

Input

Input includes multiple sets of test data.Each test data occupies one line,including two positive integers a(1≤a≤2*10^4),b(1≤b≤10^9),and their meanings are shown in the description.Contains most of the 12W test cases.

Output

For each set of input data,output a line of two integers,representing X, Y.If you cannot find such X and Y,output one line of "No Solution"(without quotation).

Sample Input

 

6 8 798 10780

Sample Output

 

No Solution 308 490

Source

2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)

题意就是给定正整数a,b。求x,y使得x+y=a && LCM(x,y)=b;测试组数最多为120000;

无脑做法就是暴力枚举a,然后找出i使得LCM(i,a-i)==b。

显然一组是没问题的,120000组就GG了。

所以就想啊,题目给出了两个方程,两个未知数,是不是可以解方程呢?答案是肯定的。

但是第一个问题来了,怎么联系x,y与LCM(x,y)呢,我们知道 LCM(x,y) =  \frac{x*y}{GCD(x,y))} 那么不妨设GCD(x,y)=k; 则原方程化为:

ck+dk=a;      cdk=b; (由于k为最大公约数,所以GCD(c,d)=1)

联立消去c 得k*d^{2} - a*d +b = 0 所以问题转化为该方程是否有解(:

那么第二个问题来了,k是GCD(x,y)也是未知的呀!

其实这里可以做一个转化:GCD(x,y)=GCD(a,b);证明如下:

GCD(a,b)=GCD(cdk,(c+d)*k) 所以只要证明GCD(cd,c+d)==1,下面用反证法证明:

加入cd与c+d不互质,即存在一个公共因子t,由于c,d互质,所以c或者d且只能是其中一个有因子t,又因为c+d有因子t,则c,d同时拥有因子t,相矛盾,故假设不成立,即cd与c+d互质!

所以上述问题得证;

所以先算一下GCD(a,b)然后带入方程计算一下就行了!

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<stack>

using namespace std;

#define pii pair<int, int>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
typedef long long ll;

template <class T> inline void read(T &x) {
	x = 0;int f = 1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x=x*f;
}

template<typename E>
E gcd(E a,E b){return b==0?a:gcd(b,a%b);}

template<typename E>
E ex_gcd(E a,E b,E &x,E&y) {if(b==0){x=1;y=0;}else {ex_gcd(b,a%b,y,x);y-=(a/b)*x;}}

template<typename E>
E quick_pow(E a,E b,E c){E ans=1;while(b){if(b&1) ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}

template<typename E>
E inv1(E a,E b){return quick_pow(a,b-2,b);}

template<typename E>
E inv2(E a,E b){E x,y;ex_gcd(a,b,x,y);return (x%b+b)%b;}

template<typename E>
E oula(E n){
	E res=n;
	for(E i=2;i*i<=n;i++){
		if(n%i==0){
			while(n%i==0) n/=i;
			res-=res/i;
		}
	}
	if(n>1) res-=res/n;
	return res;
}

const double eps=1.0e-5;
const int maxn=200000+10;
const ll mod=10007;

ll t,a,b;

int main()
{
	while(~scanf("%lld%lld",&a,&b)){
		ll k=gcd(a,b),tmp1=a*a-4*k*b,tmp2=sqrt(tmp1);
		if(tmp1>=0&&tmp2*tmp2==tmp1){
			ll d=(a+tmp2)/(2*k);
			if(((a+tmp2)%(2*k)==0)&&(b%(k*d)==0)){
				ll ans1=b/d,ans2=a-ans1;
				if(ans1>ans2) swap(ans1,ans2);
				printf("%lld %lld\n",ans1,ans2);
				continue; 
			}else{
				d=(a-tmp2)/(2*k);
				if((a-tmp2)%(2*k)==0&&(b%(k*d)==0)){
					ll ans1=b/d,ans2=a-ans1;
					if(ans1>ans2) swap(ans1,ans2);
					printf("%lld %lld\n",ans1,ans2);
					continue;
				}
			}
		}
		puts("No Solution");
	} 
}

 

传送门:http://codeforces.com/problemset/problem/1076/C

                                                                                    C. Meme Problem

                                                                            time limit per test:1 second

                                                                   memory limit per test:256 megabytes

                                                                               input:standard input

                                                                              output:standard output

Try guessing the statement from this picture:

You are given a non-negative integer dd . You have to find two non-negative real numbers aa and bb such that a+b=da+b=d and a⋅b=da⋅b=d .

Input

The first line contains tt (1≤t≤1031≤t≤103 ) — the number of test cases.

Each test case contains one integer dd (0≤d≤103)(0≤d≤103) .

Output

For each test print one line.

If there is an answer for the ii -th test, print "Y", and then the numbers aa and bb .

If there is no answer for the ii -th test, print "N".

Your answer will be considered correct if |(a+b)−a⋅b|≤10−6|(a+b)−a⋅b|≤10−6 and |(a+b)−d|≤10−6|(a+b)−d|≤10−6 .

Example

Input

7
69
0
1
4
5
999
1000

Output

Y 67.985071301 1.014928699
Y 0.000000000 0.000000000
N
Y 2.000000000 2.000000000
Y 3.618033989 1.381966011
Y 997.998996990 1.001003010
Y 998.998997995 1.001002005

这题就很简单了,直接联立解方程,注意精度问题就好了(:

AC code:

#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, int>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define per(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define PI acos(-1.0)
#define inf 0x3f3f3f3f
typedef long long ll;

template <class T> inline void read(T &x) {
	x = 0;int f = 1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x=x*f;
}

template<typename E>
E gcd(E a,E b){return b==0?a:gcd(b,a%b);}

template<typename E>
E ex_gcd(E a,E b,E &x,E&y) {if(b==0){x=1;y=0;}else {ex_gcd(b,a%b,y,x);y-=(a/b)*x;}}

template<typename E>
E quick_pow(E a,E b,E c){E ans=1;while(b){if(b&1) ans=ans*a%c;a=a*a%c;b>>=1;}return ans;}

template<typename E>
E inv1(E a,E b){return quick_pow(a,b-2,b);}

template<typename E>
E inv2(E a,E b){E x,y;ex_gcd(a,b,x,y);return (x%b+b)%b;}

const double eps=1.0e-5;
const int maxn=200000+10;
const ll mod=10007;

long double a,b,c,d,e;

void solve()
{
	b=d*d-4*d;c=sqrt(b);
	if(b>=0){
		e=(d+c)/2;
		if(e>=0){
			printf("Y %.9Lf %.9Lf\n",e,d-e);
			return;
		}
		e=(d-c)/2;
		if(e>=0){
			printf("Y %.9Lf %.9Lf\n",e,d-e);
			return;
		}
	}
	puts("N");
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%Lf",&d);
		solve();
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值