题目链接:http://lightoj.com/volume_showproblem.php?problem=1078
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 | Output for Sample Input |
3 3 1 7 3 9901 1 | Case 1: 3 Case 2: 6 Case 3: 12 |
题目大意:给你两个整数n、s,让你求最少个s (如:ss、sss等)能够整除n
解析:刚开始用搜索,一直LTE,后来听一位大牛说用同余定理,然后试着就AC了,,,,
同余定理: 所谓的同余,顾名思义,就是许多的数被一个数d去除,有相同的余数。d数学上的称谓为模。
如a=6,b=1,d=5,则我们说a和b是模d同余的。因为他们都有相同的余数1。
水题,不多说了,上代码:
第一次用DFS错误的代码:
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 10000007;
unsigned long long n, k;
int f, ans;
void dfs(unsigned long long x)
{
if(f) return ;
if(x % n == 0)
{
f = 1;
return ;
}
ans++;
dfs(x * 10 + k);
}
int main()
{
int t, cnt = 0;
cin >> t;
while(t--)
{
unsigned long long kk;
f = ans = 0; ans++;
cin >> n >> k;
kk = k;
dfs(kk);
printf("Case %d: ", ++cnt);
cout << ans << endl;
}
return 0;
}
第二次用同余定理AC的代码:
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 10000007;
int main()
{
int t, cnt = 0, n, k;
cin >> t;
while(t--)
{
scanf("%d%d", &n, &k);
int ans = 1, kk = k;
while(kk % n != 0)
{
kk = (kk % n) * 10 + k;
ans++;
}
printf("Case %d: %d\n", ++cnt, ans);
}
return 0;
}