24h購物| | PChome| 登入
2012-09-02 13:43:19| 人氣780| 回應0 | 上一篇 | 下一篇

[UVA] 10677 - Base Equality

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

Problem E
Base Equality

Input:
standard input
Output:
standard output
Time Limit:
1 second

 

Numerous are the moments I as a programmer have been frustrated by the tedious conversions between decimal numbers and hexadecimal ones. Why have we chosen 10 as a base in our everyday numerical presentations, when 16 seems so practically appealing? Obviously because everyone is not the computer geek I am. Maybe some day the world will fully realise the benefits of the hexadecimal system. In the meantime I have to learn to master the base conversions since most of the time numbers do not resemble one another in different bases.

Sometimes peculiar relationships emerge among the different base representations of numbers though. For instance, I noticed just the other day that 104010 * 4 = 104016, i.e. (1*103+0*102+4*101+0*100)*4=(1*163+0*162+4*161+0*160). It made me wonder how often this is the case, that is, the digits of a number in one base, are exactly the same as the digits of a multiple of the number in another base. Formally, let B1<B2 be positive integers, and a0,a1,…,ak be integers in [0…B1-1]. For which ai’s is there a positive integer c such that

                                            

Input

On the first line of input is a positive integer n telling the number of test cases that follow. Each test case is on a line of its own and consists of two integer bases B1, B2, 9<B1<B2<100, and two integer range elements, r1, and r2, 0<r1<r2<10000. Notice that all numbers in the input are given in the base 10.

Output

For each test case, there should be one row containing the largest integer i, fulfilling r1<i<r2, for which there is a positive integer c such that the digits of i in the base B1, are exactly the same as the digits of i*c in the base B2. If no such integer i exists, output the text ‘Non-existent.’.

 

Sample Input                             Output for Sample Input

4                          

10 16 1 2000

10 16 1 4999

10 14 10 9999

11 14 10 9999

1040

4240

Non-existent.

9240




#include <stdio.h>

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int B1, B2, r1, r2, i;
        scanf("%d %d %d %d", &B1, &B2, &r1, &r2);
        for(i = r2-1; i > r1; i--) {
            int ch[20], j = i, idx = 0;
            while(j) {
                ch[idx] = j%B1;
                j /= B1;
                idx++;
            }
            int base = 1, sum = 0;
            for(j = 0; j < idx; j++)
                sum += ch[j]*base, base *= B2;
            if(sum%i == 0) {
                printf("%d\n", i);
                break;
            }
        }
        if(i == r1)
            puts("Non-existent.");
    }
    return 0;
}

台長: Morris
人氣(780) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[ACM-ICPC][Asia - Daejeon] 5841 - Color Length
此分類上一篇:[UVA] 626 - Ecosystem

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