Xtuoj A + B again (find the minimum approximate number greater than m in a number)

Source: Internet
Author: User

Freshman competition:
Accepted: 15   Submit: 243
Time Limit: 1000 MS   Memory limit: 65536 KB
Description

James had a lot of students who didn't like to think about a + B in the last semi-finals. To apologize, James made a simple A + B Question for everyone, I hope you will be happy to answer questions. So, the question is coming !!!
Evaluate the value of X, the smallest positive integer that makes B/(a + x) an integer.

Input

The first line is an integer k (K ≤ 10000), indicating the number of samples. In the following example, each row contains two positive integers, A and B (1 ≤ a, B ≤ 108 ).

Output

Output the result of a sample in each row. If there is no such X, the output is-1.

Sample Input
 
31 21 31 4
Sample output
 
121
Idea: I started to find out all the prime factor complexity logn of B, and then find the approximate number generated for each prime factor, that is, the prime factor M * (, 3 ...) (The complexity is too high) It is similar to the screening method. In sort, scan and find the minimum approximate number greater than a. The result is t, and the data size of 10 ^ 7 is T. Therefore, you do not need to generate all the approximate numbers. Each prime factor only needs to generate a minimum approximate number greater than, in this way, an approximate number (less than ologn) with the same number as the prime factor is generated, and then sort can be used. The Code is as follows:
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <map> # include <algorithm> # include <set> # include <vector> using namespace STD; int A, B, Su; vector <int> S; vector <int> All; void solve () {int temp = B; if (a> = temp) {cout <"-1" <Endl; return;} // Special Judgment // prime factor: For (INT I = 2; I * I <= temp; I ++) {If (TEMP % I = 0) {S. push_back (I); While (TEMP % I = 0) temp/= I ;}} S. push_back (B); // because bitself may be a prime number, add it to the vector. B. The result is not affected even if it is not a prime number. For (INT I = 0; I <S. Size (); I ++) // generate the S. Size () approx. {Su = s [I]; int M = A/Su + 1; All. push_back (Su * m);} Sort (all. begin (), all. end (); For (INT I = 0; I <all. size (); I ++) if (ALL [I]> A) {cout <all [I]-A <Endl; return ;}} int main () {int _; CIN >>_; while (_ --) {CIN >>> A> B; S. clear (); All. clear (); solve ();} return 0 ;}

Xtuoj A + B again (find the minimum approximate number greater than m in a number)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.