LightOJ - 1078
题目:
If an integer is not divisible by 2 or 5, some multiple of that number in decimal notation is a sequence of only a digit. Now you are given the number and the only allowable digit, you should report the number of digits of such multiple.
For example you have to find a multiple of 3 which contains only 1's. Then the result is 3 because is 111 (3-digit) divisible by 3. Similarly if you are finding some multiple of 7 which contains only 3's then, the result is 6, because 333333 is divisible by 7.
Input
Input starts with an integer T (≤ 300), denoting the number of test cases.
Each case will contain two integers n (0 < n ≤ 106 and n will not be divisible by 2 or 5) and the allowable digit (1 ≤ digit ≤ 9).
Output
For each case, print the case number and the number of digits of such multiple. If several solutions are there; report the minimum one.
Sample Input
3
3 1
7 3
9901 1
Sample Output
Case 1: 3
Case 2: 6
Case 3: 12
题意:给出 n和d,让判断多少位的d可以整除n(每一位的数值都为d,如果当前的位数不能整除n则再加上一位)
分析:
可怜的java大数超时
正解 同余定理
(A + B) mod M = ( A mod M + B mod M ) mod M
(A * B) mod M = ((A mod M) *( B mod M)) mod M
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+5;
int a[N];int main(){int T;scanf("%d",&T);int cas=0;while(T--){int n,m;scanf("%d%d",&n,&m);int ans=m;for(int i=1;;i++){if(ans%n==0){ans=i;break;}else{ans=(ans*10+m)%n;}}printf("Case %d: %d\n",++cas,ans);}
}
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {public static void main(String[] args) throws IOException {Scanner in = new Scanner (new BufferedInputStream(System.in));//Scanner in = new Scanner(new BufferedInputStream(System.in));Reader.init(System.in);int T=in.nextInt();int cas=0;while(T-->0){cas++;BigInteger n=in.nextBigInteger(); BigInteger m=in.nextBigInteger();BigInteger t=m;int i=1;for(i=1;;i++){BigInteger t1=t;//System.out.println(t1.mod(n));if(t.mod(n).equals(new BigInteger("0"))){break;}else{t=t.multiply(new BigInteger("10")).add(m);}}System.out.println("Case "+cas+": "+i+"\n");}}}
class Reader
{ static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader(new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while(!tokenizer.hasMoreTokens()) { String str=reader.readLine();//System.out.println(str);tokenizer = new StringTokenizer(str); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); }
}