-
Notifications
You must be signed in to change notification settings - Fork 0
/
reciprocal-cycles.cpp
75 lines (69 loc) · 1.34 KB
/
reciprocal-cycles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// https://www.hackerrank.com/contests/projecteuler/challenges/euler026/problem
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll fun(ll x)
{
ll count = 1;
ll check[x] = {0};
check[1] = count++;
ll i = 1;
while( i < x)
{
i *= 10;
}
i = i % x;
//cout << i << endl;
while(i!=0 && check[i] == 0)
{
//cout << i << endl;
check[i] = count ++;
while( i < x)
{
i *= 10;
}
i = i % x;
}
return count - check[i];
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
bool prime[10001] = {0};
prime[0] = true;
prime[1] = true;
for(ll i= 2; i* i<=10000 ;i ++)
{
if(!prime[i])
{
for(ll j = i * i; j<=10000; j +=i)
prime[j] = true;
}
}
ll ans[10001];
ll mx = 1;
ll p = 3;
ans[4] = 3;
// cout << fun(7) << endl;
for(ll i = 5; i<=10000; i++)
{
ans[i] = p;
if(!prime[i])
{
ll value = fun(i);
if(value > mx)
{
mx = value;
p = i;
}
}
}
ll t;
cin >> t;
while( t-- )
{
ll n;
cin >> n;
cout << ans[n] << endl;
}
return 0;
}