Almost
For two integers a and c, we define a and a third integer b to be almost factors of c by choosing the minimal b that minimizes |ab-c|. Your task will be to determine b given a and c.
Input
Your input file will consist of a single integer N, the number of test cases in the file, followed by N pairs of integers a and c. All tokens in the input will be separated by some whitespace.
Output
Your output should consist of N newline-separated integers, each one representing b for the corresponding pair (a, c) in the input.
Constraints
5 ≤ N ≤ 20
1 ≤ a, c ≤ 1010
#include <iostream>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <cstdio>
typedef long unsigned long lli;
using namespace std;
main(){
int n,i,min;
lli a[2],b,bef;
cin >> n;
while(n--){
cin>>a[0]>>a[1];
sort(a, a+2);
bef = 10000000000LL;
for(i=1,min=1;;i++){
b=llabs(a[1]-a[0]*(lli)i);
if(bef>b)bef=b,min=i;
if(bef<b)break;
}
cout<<min<<endl;
}
}
No comments:
Post a Comment