24h購物| | PChome| 登入
2012-07-09 19:52:37| 人氣841| 回應0 | 上一篇 | 下一篇

[UVA] 1226 - Numerical surprises

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

We suspect that for every positive integer N there exists an integer of the form 11...10...0 (a sequence of 1's followed by 0 or more 0's) that is divisible by N . For example, with N = 3 , 111 is divisible by 3, with N = 4 , 100 is divisible by 4, with N = 7 , 11111 is divisible by 7. We want to verify this for some integers. The solution to this problem is to find two different numbers P and Q in the form of 11...1 (a sequence of 1's) that have the same remainder when dividing by N . The difference D between P and Q will be in the form of 11...10...0 and divisible by N .

In order to solve this problem, we have to start with finding the remainder when dividing a number in the form of 11...1 by N . Your task is to write a program to do this.

Input 

The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

Each data set is described by two lines. The first line contains the integer N (1 < N < 109) . The second line contains the integer number P (P contains at least one digit and at most 2000 digits).

Output 

For each test case, write in one line the remainder when dividing P by N .

Sample Input 

2
4 
11 
5 
111

Sample Output 

3 
1


#include <stdio.h>

int main() {
    int t, n, i;
    char s[3000];
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        scanf("%s", s);
        int tmp = 0;
        for(i = 0; s[i]; i++) {
            tmp = tmp*10 + s[i]-'0';
            tmp %= n;
        }
        printf("%d\n", tmp);
    }
    return 0;
}

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        BigInteger a, b;
        int t;
        t = cin.nextInt();
        while(t-- != 0) {
            a = new BigInteger(cin.next());
            b = new BigInteger(cin.next());
            System.out.println(b.mod(a).toString());
        }
    }
}

台長: Morris
人氣(841) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Java] 10198 - Counting
此分類上一篇:[UVA][樹形DP] 10459 - The Tree Root

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