A+b Again
Accepted:15 submit:243
Time limit:1000 MS Memory limit:65536 KB
Title Description
Last fun Sai Xiaoming a+b pit A lot of students do not like to think, Xiao Ming in order to apologize, this time out a simple a+b to everyone when check-in questions, I hope we can be happy brush questions. So, the problem comes!!!
The value of the smallest positive integer x that makes B/(a+x) an integer.
Input
The first line is an integer K (k≤10000) that represents the number of samples. For each subsequent line, a two positive integer, A, B (1≤a,b≤108).
Output
Each row outputs the result of a sample, if there is no such x, output-1.
Sample input
3
1 2
1 3
1 4
Sample output
1
2
1
Master's idea: I began to find all the factorization complexity of B logn, and then asked each factorization generated by the approximate factorization m* (... (The complexity is large) and the idea of the sieve is similar,
Again sort, sweep again to find the smallest number greater than a, the result is T, in the 10^7 data size is T.
So change, do not need to generate all the approximate, each factorization as long as the generation of a minimum number of more than a can be, so that there is a total amount of the same as a mass factor of approximate (less than OLOGN), and then sort.
Acknowledge:http://94it.net/a/jingxuanboke/2015/0112/446788.html
1 /*2 idea: I began to find all the factorization complexity of B logn, and then asked each factorization to generate the approximate factorization m* (... (The complexity is large) and the idea of the sieve is similar,3 again sort, sweep again to find the smallest number greater than a, the result is T, in the 10^7 data size is T. 4 so change, do not need to generate all the approximate, each factorization as long as the generation of a minimum number of more than a can be, so that there is a total amount of the same as a mass factor of approximate (less than OLOGN), and then sort. 5 */6#include <iostream>7#include <cstdio>8#include <cstring>9#include <string>Ten#include <map> One#include <algorithm> A#include <Set> -#include <vector> - using namespacestd; the intA,b,su; -vector<int>s; -vector<int>All ; - voidSolve () + { - inttemp=b; + if(a>=temp) {cout<<"-1"<<endl;return; }//Special Award A //Find factorization: at for(intI=2; i*i<=temp;i++) -{if(temp%i==0) {s.push_back (i); while(temp%i==0) temp/=i;} } -S.push_back (b);//because B itself may be a prime number, adding B to the vector, even if it is not a prime number, has no effect on the result. - for(intI=0; I<s.size (); i++)//generates an approximate s.size (). -{Su=s[i];intm=a/su+1; All.push_back (su*m); } - sort (All.begin (), All.end ()); in for(intI=0; I<all.size (); i++) - if(all[i]>a) to{cout<<all[i]-a<<endl;return ;} + } - intMain () the { *Freopen ("B.txt","R", stdin); $ intT cin>>t; while(t--) {cin>>a>>b; s.clear (); All.clear (); Solve ();}return 0;Panax Notoginseng}
Queue
A+b Again (Find the smallest number greater than m in some numbers)