Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
简单解法即证明import java.util.*; import java.io.*; import java.lang.Math; public class Main{ static long gcd(long a,long b) { long tmp; while(b!=0) { a%=b; tmp=a; a=b; b=tmp; } return a; } static long lcm(long a,long b) { return a/gcd(a,b)*b; } public static void main(String args[]){ Scanner cin = new Scanner (System.in); long a,b,c,i; while(cin.hasNext()) { a=cin.nextLong(); b=cin.nextLong(); c=b/a; for(i=(long)Math.sqrt(c);i>=1;i--)//a*i+b/i随i的增大而减小 { if(c%i==0&&gcd(i,c/i)==1) { System.out.println((a*i)+" "+(b/i));//b/i肯定大于a*i break; } } } } } 问:为什么a*i+b/i随i的增大而减小? 答:i<=sqrt(c)=sqrt(b/a),设f(i)=a*i+b/i求导:f'(i)=a-b/(i*i) 因为i<=sqrt(b/a),所以f'(i)<=0 所以所以i越大f(i)越小,即两个数的和就越小,所以直接从i的最大值开始枚举,一旦符合条件就输出,另外a*i也肯定是大于b/i的 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator