欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Sqrt Approaching (数学推导+放缩)

程序员文章站 2022-04-16 23:48:10
原题题面Given three positive integers A,B,nA,B,nA,B,n, where nnn is NOT a perfect square number, you should find two positive integers C,DC,DC,D satisfying (BC−AD)(C−Dn)<0(BC - AD)(C - D\sqrt{n}) < 0(BC−AD)(C−Dn​)<0输入格式The first line contains one i...

原题题面

Given three positive integers A , B , n A,B,n A,B,n, where n n n is NOT a perfect square number, you should find two positive integers C , D C,D C,D satisfying ( B C − A D ) ( C − D n ) < 0 (BC - AD)(C - D\sqrt{n}) < 0 (BCAD)(CDn )<0

输入格式

The first line contains one integer A ( 1 ≤ A < 1 0 99990 ) . A(1\leq A<10^{99990}). A(1A<1099990).
The second line contains one integer B ( 1 ≤ B < 1 0 99990 ) . B(1≤B<10^{99990}). B(1B<1099990).
The third line contains one integer n ( 2 ≤ n ≤ 1 0 9 ) . n(2≤n≤10^{9}). n(2n109).
It is guaranteed that n n n is NOT a perfect square number.

输出格式

The first line contains one integer C ( 1 ≤ C < 1 0 1 0 5 ) . C (1≤C<10^{10^{5}}). C(1C<10105).
The second line contains one integer D ( 1 ≤ D < 1 0 1 0 5 ) . D(1≤D<10^{10^{5}}). D(1D<10105).

输入样例

2
1
2

输出样例

3
2

题面分析

想了很久的数学题。
给定三个正整数 A , B , n A,B,n A,B,n,保证 n n n不是完全平方数,求一对正整数 C , D C,D C,D使
( B C − A D ) ( C − D n ) < 0 (BC - AD)(C - D\sqrt{n}) < 0 (BCAD)(CDn )<0
成立。
保证有解,有多个时输出其中一组。
首先不难得到 m i n ( A B , n ) < C D < m a x ( A B , n ) min(\frac{A}{B},\sqrt{n})<\frac{C}{D}<max(\frac{A}{B},\sqrt{n}) min(BA,n )<DC<max(BA,n )
很容易就可以明白 C , D C,D C,D有无穷多组解,证明略。
因此本题的上下界比较宽松。
假设 A B < n \frac{A}{B}<\sqrt{n} BA<n ,我们可以构造 C D = X n + D B n + Y ( X ≥ A ) \frac{C}{D}=\frac{Xn+D}{Bn+Y}(X\geq A) DC=Bn+YXn+D(XA),易知存在 C , D C,D C,D使得 C D > A B \frac{C}{D}>\frac{A}{B} DC>BA
故考虑如何构造使得 C D < n \frac{C}{D}<\sqrt{n} DC<n
C D − n \frac{C}{D}-\sqrt{n} DCn
= X n + D − n ( B n + Y ) B n + Y =\frac{Xn+D-\sqrt{n}(Bn+Y)}{Bn+Y} =Bn+YXn+Dn (Bn+Y)
= ( X n − n B n ) + ( D − n Y ) B n + Y < 0 =\frac{(Xn-\sqrt{n}Bn)+(D-\sqrt{n}Y)}{Bn+Y}<0 =Bn+Y(Xnn Bn)+(Dn Y)<0
不妨令 D = 0 D=0 D=0,即满足 ( X n − n B n − n Y ) < 0 (Xn-\sqrt{n}Bn-\sqrt{n}Y)<0 (Xnn Bnn Y)<0
( X n − B n − Y ) < 0 (X\sqrt{n}-Bn-Y)<0 (Xn BnY)<0
等价于 ( B n ( 1 − n ) + ( X − B ) n − Y ) < 0 (B\sqrt{n}(1-\sqrt{n})+(X-B)\sqrt{n}-Y)<0 (Bn (1n )+(XB)n Y)<0
等价于 ( B n ( 1 − n ) − ( X − B ) ( 1 − n ) + ( X − B − Y ) ) < 0 (B\sqrt{n}(1-\sqrt{n})-(X-B)(1-\sqrt{n})+(X-B-Y))<0 (Bn (1n )(XB)(1n )+(XBY))<0
那只需要 X − B − Y < = 0 X-B-Y<=0 XBY<=0即可,不妨 X = A + B , Y = A X=A+B,Y=A X=A+B,Y=A
得到 C D = ( A + B ) n B n + A \frac{C}{D}=\frac{(A+B)n}{Bn+A} DC=Bn+A(A+B)n
经验证,得到其为正确解。
A B > n \frac{A}{B}>\sqrt{n} BA>n 时推导类似,这里不再赘述。

AC代码(1232ms)

import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		BigInteger A = input.nextBigInteger();
		BigInteger B = input.nextBigInteger();
		BigInteger n = input.nextBigInteger();
		BigInteger C = n.multiply((A.add(B)));
		BigInteger D = n.multiply(B).add(A);
		System.out.println(C);
		System.out.println(D);
	}
}

后记

用Python写的话应该可以更快…Java真拉胯
DrGilbert 2020.10.25

本文地址:https://blog.csdn.net/oampamp1/article/details/109276824

相关标签: acm竞赛 数学