[UVA] 1226 - Numerical surprises
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());
}
}
}