It took four hours race do not get out of this problem, the result after the game found \ (p = 3 \) special judge was wrong, weeping
\(A(i^j)=\sum_{k=0}^{i^j-1}10^k=\frac{10^{i^j}-1}{9}\equiv0(mod\ p)\)
When \ (p \ neq3 \) when (designated key ① !!!), there are \ (10 ^ {i ^ j } \ equiv1 (mod \ p) \)
Taking into account Fermat's little theorem, when \ (gcd (10, p) = 1 \) , that \ (p \ neq 2 \) and \ (p \ neq5 \) when (designated focus ② !!!), there is \ (10 ^ {i ^ j- (p-1)} \ equiv10 ^ {i ^ jk (p-1)} \ equiv ... \ equiv10 ^ {i ^ j \% (p-1)} \ equiv1 \ equiv10 ^ 0 (mod \ p ) \)
So \ (the p-1-\ | \ i ^ J \) .
But \ (p-1 \) is not necessarily that the \ (10 ^ x \ equiv1 ( mod \ p) \) is the smallest positive integer \ (X \) , so we set \ (Q \) is the smallest \ ( X \) , so \ (10 ^ q \ equiv1 ( mod \ p) \)
Obviously we have \ (q \ | \-the p-1 \) , so there \ (q \ | \ i ^ J \) .
This \ (Q \) can \ (O (\ sqrt {p } log_2p) \) time complexity is obtained violence.
The question then converted to the number of \ (1 \ leq i \ leq n, 1 \ leq j \ leq m \) such that \ (Q \ | \ I ^ J \) .
Consider \ (Q \) prime factorization of formula \ (P_1 ^ Q = {P_2 E_1} ^ {} ... p_x E_2 e_x} ^ {\) , we set up a set of \ (P_q = \ {p_1, p_2 ,. .., p_x \} \)
Consider \ (I \) prime factorization of formula \ (^ I = {R_1 R_2 D_1} ^ {} ... r_y D_2 D_y, ^ {} \) , set \ (P_i = \ {r_1, r_2, .. ., r_y \} \) , apparently only when the \ (P_i \ supset P_q \) when is it possible to have a \ (q \ | \ i ^ J \) .
Convenience, rewriting \ (I \) decomposition of formula \ (i = p_1 ^ {d_1 } p_2 ^ {d_2} ... p_x ^ {d_x} p_ {x + 1} ^ {d_ {x + 1}}. } ^ {D_y, ..p_y \) .
Now consider this question: We fixed \ (i \) , how many \ (j \) satisfy the condition?
Obviously, \ (\ FORALL K \ Leq X, \) have \ (jd_k \ GEQ E_k \) , so \ (J \ GEQ \ lceil \ FRAC {E_k} {d_k} \ rceil \) , thus satisfying the condition \ ( J \) have \ (m- {max} _ { k = 1} ^ x \ lceil \ frac {e_k} {d_k} \ rceil + 1 \) a.
Therefore enumeration \ (i \) to count \ (j \) of the total complexity of the worst dropped \ (O (nlogn) \) (?)
Next we enumerate \ (p_1, p_2, ..., p_x \) index \ (D_1, d_2, ..., D_X \) , consider the number of \ (1 \ leq i \ leq n \) makes its prime factorization of formula \ (i = p_1 ^ {f_1 } p_2 ^ {f_2} ... p_x ^ {f_x} p_ {x + 1} ^ {f_ {x + 1}} ... p_y ^ {f_y } \) in \ (= F_1 D_1, D_2 = F_2, ..., D_X F_X = \) .
The inclusion and exclusion may be done easily determined \ (f_1 \ geq d_1, f_2 \ geq d_2, ..., f_x \ geq d_x \) a \ (I \) number is \ (\ lfloor \ frac {n } {p_1 ^ {d_1} p_2 ^ {d_2} ... p_x ^ {d_x}} \ rfloor \)
Receiving denounced simple calculations \ (f_k \ geq d_k + 1 \) a \ (I \) number, can calculate the final results.
the complexity? I would not count, but because \ (p \) will not be more than 8, so visually \ (O (\) can live \ () \) ...... I just ran 12ms ......
Overall complexity \ (O (\ sqrt plog_2p + \) can live \ () \) . (???)
#include <bits/stdc++.h>
#define LL long long
#define MAXN 100008
using namespace std;
LL p, n, m;
LL q;
LL qp(LL a, LL b, LL mod)
{
LL ret = 1;
while (b)
{
if (b & 1)
ret = ret * a % mod;
a = a * a % mod;
b >>= 1;
}
return ret;
}
LL prime[18], ans;
int e[18];
int primelen = 0;
int d[18];
LL getval(LL msk, LL prod)
{
int cnt1 = 0;
for (int i = 0; i < primelen; ++i)
{
if (msk & (1 << i))
{
cnt1++;
prod *= prime[i + 1];
if (prod > n)
return 0;
}
}
if (cnt1 % 2)
return - (n / prod);
else
return n / prod;
}
void dfs(int d[], int i, LL prod)
{
if (prod > n)
return;
if (i > primelen)
{
LL ret = 0;
for (int msk = 0; msk < (1 << primelen); ++msk)
{
ret += getval(msk, prod);
}
//cout << "ret = " << ret << endl;
int mx = -1;
for (int j = 1; j <= primelen; ++j)
{
mx = max(mx, (int)ceil(e[j] * 1.0 / d[j]));
}
//cout << "mx = " << mx << endl;
ans += max(0LL, m - mx + 1) * ret;
return;
}
LL cprod = prod;
for (d[i] = 1; cprod * prime[i] <= n; ++d[i])
{
cprod *= prime[i];
dfs(d, i + 1, cprod);
}
return;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
ans = 0;
memset(prime, 0, sizeof(prime));
memset(d, 0, sizeof(d));
memset(e, 0, sizeof(e));
primelen = 0;
q = 0x3f3f3f3f;
scanf("%lld %lld %lld", &p, &n, &m);
for (LL i = 1; i * i <= p - 1; ++i)
{
if ((p - 1) % i)
continue;
if (qp(10, i, p) == 1)
q = min(q, i);
if (qp(10, (p - 1) / i, p) == 1)
q = min(q, (p - 1) / i);
}
//cout << "q = " << q << endl;
LL temp = q;
for (int i = 2; i * i <= temp; ++i)
{
if (temp % i == 0)
{
prime[++primelen] = i;
while (temp % i == 0)
{
temp /= i;
e[primelen]++;
}
}
}
if (temp > 1)
{
prime[++primelen] = temp;
temp = 1;
e[primelen] = 1;
}
dfs(d + 1, 1, 1);
if (p == 2 || p == 5 || q >= 0x3f3f3f3f)
ans = 0;
else if (p == 3)
ans = (n / 3) * m;
printf("%lld\n", ans);
}
}