24h購物| | PChome| 登入
2012-12-16 18:50:12| 人氣1,288| 回應0 | 上一篇 | 下一篇

[UVA][因數] 12573 - Sohel Sir's Assignment

推薦 0 收藏 0 轉貼0 訂閱站台


  Sohel Sir's Assignment 

Sohel sir gave an assignment in CSE-315 course instead of a class test. The assignment was to make questions and provide corresponding answers from the chapters 2, 3, 4, 5. Each student is assigned chapter no y according to the formula:

y = (Roll % 4) + 2
I.e. he has to make questions and answers from chapter y. According to this rule, Roll 4 was supposed to make questions and answers from chapter 2 as (4 % 4) + 2 = 2 and Roll 35 was assigned to chapter 5 as (35 % 4) +2 = 5. In the meantime, roll 35 had already made the questions & answers from chapter 5 and Roll 4 got the complete assignment of roll 35. So to copy that assignment Roll 4 wanted to change the divisor 4 of the formula to some number m such that his assignment changes to chapter 5 , that is (4 % m) + 2 = 5. But he failed to find such number. Now, your problem is similar to the above problem.

Given two number x and y you have to find a positive number m such that (x % m) + 2 = y. If multiple m is possible, choose the minimum one. If no answer is found print `Impossible'.

Input 

First line of input will contain the number of test cases, T$ le$125. Then there follows T lines, each containing two integers x ( 0$ le$x$ le$1012) and y ( 2$ le$y$ le$x + 2).

Output 

For each case, print m, if m is found. Otherwise print `Impossible' (without quotes). See the samples given below for exact formatting.

Sample Input 

4
4 5
35 5
4 2
11 5

Sample Output 

Impossible
4
1
4


Problem Setter: Mohammad Hafiz Uddin
Alternate Solution: Radi Muhammad Reza



x%m+2  = y
x%m = y-2
x = km + y-2
x-y+2 = km
分解 x-y+2 即可,如果 x-y+2 == 0,輸出 x+1。

#include <stdio.h>
#include <math.h>
int main() {
    scanf("%*d");
    long long x, y;
    int i;
    while(scanf("%lld %lld", &x, &y) == 2) {
        long long n = x-(y-2), sq;
        sq = (long long)sqrt(n);
        if(n == 0) {
            printf("%lld\n", x+1);
            continue;
        }
        int flag = 0;
        long long mn;
        for(i = 1; i <= sq; i++) {
            if(n%i == 0) {
                if(x%i+2 == y) {
                    if(!flag)   mn = i, flag = 1;
                    if(i < mn)  mn = i;
                }
                if(x%(n/i)+2 == y) {
                    if(!flag)   mn = n/i, flag = 1;
                    if(n/i < mn)  mn = n/i;
                }
            }
            if(flag && i > mn)  break;
        }
        if(flag)
            printf("%lld\n", mn);
        else
            puts("Impossible");
    }
    return 0;
}


台長: Morris
人氣(1,288) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][math] 580 - Critical Mass
此分類上一篇:[UVA][bitmask+背包] 10032 - Tug of War

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文