Submission #56646456
Source Code Expand
#include <bits/stdc++.h>
#define fir first
#define sec second
#ifdef LOCAL
#define dbg(x) cerr << "In Line " << __LINE__ << " the " << #x << " = " << x << '\n'
#define dpi(x, y) cerr << "In Line " << __LINE__ << " the " << #x << " = " << x << " ; " << "the " << #y << " = " << y << '\n'
#define dbgf(fmt, args...) fprintf(stderr, fmt, ##args)
#else
#define dbg(x) void()
#define dpi(x, y) void()
#define dbgf(fmt, args...) void()
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using vi = vector<int>;
using vpii = vector<pii>;
bool Mbe;
constexpr int MOD = 998244353;
template<typename T> T add(T a, T b, T p = MOD) { return (a + b >= p) ? (a + b - p) : (a + b); }
template<typename T> T del(T a, T b, T p = MOD) { return (a - b < 0) ? (a - b + p) : (a - b); }
template<typename T> T mul(T a, T b, T p = MOD) { return 1ll * a * b % p; }
template<typename T> T cadd(T &a, T b, T p = MOD) { return a = add(a, b, p); }
template<typename T> T cdel(T &a, T b, T p = MOD) { return a = del(a, b, p); }
template<typename T> T cmul(T &a, T b, T p = MOD) { return a = mul(a, b, p); }
template<typename T> bool cmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> bool cmin(T &a, T b) { return a > b ? a = b, true : false; }
namespace FastIO {
constexpr int LEN = 1 << 20;
char in[LEN + 1], out[LEN + 1];
char *pin = in, *pout = out, *ein = in, *eout = out + LEN;
char gc() { return pin == ein && (ein = (pin = in) + fread(in, 1, LEN, stdin), ein == in) ? EOF : *pin ++; }
void pc(char c) { pout == eout && (fwrite(out, 1, LEN, stdout), pout = out); (*pout ++) = c; return; }
void Flush() { fwrite(out, 1, pout - out, stdout); pout = out; }
template<typename T> T Read() {
T x = 0; int f = 1; char ch = gc();
while (ch < '0' || ch > '9') f = (ch == '-' ? (~f + 1) : f), ch = gc();
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
return x * f;
}
template<typename T> void Write(T x, char c) {
static char stk[40]; int tp = 0;
if (x < 0) pc('-'), x = ~x + 1;
do stk[tp++] = x % 10 + 48, x /= 10; while (x);
while (tp --) pc(stk[tp]); pc(c); return;
}
void Read(char *s) {
char ch = gc();
while (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9'))) ch = gc();
while ((ch != EOF) && ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9'))) *s = ch, s ++, ch = gc();
*s = '\0'; return;
}
void Write(char *s) {
while (*s != '\0') pc(*s), s ++; return;
}
void Puts(char *s) {
Write(s), pc('\n'); return;
}
}
#define getchar FastIO::gc
#define putchar FastIO::pc
#define Flush FastIO::Flush
#define Read FastIO::Read
#define Write FastIO::Write
#define Puts FastIO::Puts
constexpr int N = 1e6 + 5;
int n, q, a[N], b[N], phi[N];
vector<int> pri, factor[N];
map<int, int> his;
bool vis[N];
void Init() {
phi[1] = 1;
for (int i = 2; i <= n; i ++) {
if (!vis[i]) pri.emplace_back(i), phi[i] = i - 1;
for (auto j : pri) {
if (1ll * i * j > n) break; vis[i * j] = true;
if (!(i % j)) { phi[i * j] = phi[i] * j; break; }
phi[i * j] = phi[i] * phi[j];
}
}
for (auto p : pri)
for (int i = p; i <= n; i += p)
factor[i].emplace_back(p);
return;
}
int Gcd(int n, int a) {
int d = 1, g;
while ((g = __gcd(a, n)) ^ 1) d *= g, n /= g;
return d;
}
ll Pow(ll a, ll b, ll p) {
ll ans = 1;
for (; b; b >>= 1, a = (i128)a * a % p)
if (b & 1) ans = (i128)ans * a % p;
return ans;
}
pair<ll, vector<int>> Get_Phi(int a, int b) {
vector<pair<int, int>> fa, fb;
ll phi = 1ll * a * b; vector<int> vc;
for (auto p : factor[a]) {
fa.emplace_back(p, 0);
while (!(a % p)) fa.back().sec ++, a /= p;
}
for (auto p : factor[b]) {
fb.emplace_back(p, 0);
while (!(b % p)) fb.back().sec ++, b /= p;
}
int p = 0, q = 0;
while (p < fa.size() && q < fb.size()) {
int pr, cnt;
if (fa[p].fir == fb[q].fir) {
pr = fa[p].fir, cnt = fa[p].sec + fb[q].sec;
p ++, q ++;
} else if (fa[p].fir < fb[q].fir) {
pr = fa[p].fir, cnt = fa[p].sec; p ++;
} else {
pr = fb[q].fir, cnt = fb[q].sec; q ++;
}
if (cnt > 1) vc.emplace_back(pr);
for (auto pri : factor[pr - 1])
vc.emplace_back(pri);
phi /= pr, phi *= (pr - 1);
}
while (p < fa.size()) {
int pr = fa[p].fir, cnt = fa[p].sec; p ++;
if (cnt > 1) vc.emplace_back(pr);
for (auto pri : factor[pr - 1])
vc.emplace_back(pri);
phi /= pr, phi *= (pr - 1);
}
while (q < fb.size()) {
int pr = fb[q].fir, cnt = fb[q].sec; q ++;
if (cnt > 1) vc.emplace_back(pr);
for (auto pri : factor[pr - 1])
vc.emplace_back(pri);
phi /= pr, phi *= (pr - 1);
}
sort(vc.begin(), vc.end());
vc.erase(unique(vc.begin(), vc.end()), vc.end());
return {phi, vc};
}
int Solve(int N, int A, int B) {
if (!A) return 1; if (A == 1) return __gcd(N, B);
{
int d = Gcd(N, A), g;
if (d != 1) {
int _N = N / d, _A = A % _N,
_B = 1ll * Pow(A, N, N) * B % _N;
return Solve(_N, _A, _B);
}
};
int g = __gcd(A - 1, B), _A = (A - 1) / g, _N = N / Gcd(N, _A), ans = 0;
auto [Phi, vc] = Get_Phi(N, _A);
function<void(int, ll)> calc = [&](int n, ll C) {
if (his.count(n)) return; his[n] = 1;
ll mod = 1ll * N * _A / n;
for (auto p : vc) while (!(C % p) && Pow(A, C / p, mod) <= 1) C /= p;
ans += 1ll * N / _N * phi[_N / n] / C;
for (auto p : factor[_N / n]) calc(n * p, C);
return;
};
return his.clear(), calc(1, Phi), ans;
}
void slv() {
n = Read<int>(), q = Read<int>(), Init();
while (q --) {
int a = Read<int>(), b = Read<int>();
Write(Solve(n, a, b), '\n'), Flush();
}
return;
}
void clr() {
return;
}
bool Med;
int main() {
#ifdef LOCAL
freopen("!in.in", "r", stdin);
freopen("!out.out", "w", stdout);
fprintf(stderr, "%.3lf Mb\n", fabs((&Mbe - &Med) / 1048576.0));
#endif
int T = 1;
// int T = Read<int>();
while (T --) slv(), clr();
Flush();
#ifdef LOCAL
fprintf(stderr, "%d ms\n", (int)clock());
#endif
return 0;
}
Submission Info
Submission Time
2024-08-13 09:19:21+0900
Task
F - Graph of Mod of Linear
User
definieren
Language
C++ 20 (gcc 12.2)
Score
1000
Code Size
6252 Byte
Status
AC
Exec Time
2523 ms
Memory
64952 KiB
Compile Error
Main.cpp: In function ‘void FastIO::Write(T, char)’:
Main.cpp:56:17: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
56 | while (tp --) pc(stk[tp]); pc(c); return;
| ^~~~~
Main.cpp:56:44: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
56 | while (tp --) pc(stk[tp]); pc(c); return;
| ^~
Main.cpp: In function ‘void FastIO::Write(char*)’:
Main.cpp:67:17: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
67 | while (*s != '\0') pc(*s), s ++; return;
| ^~~~~
Main.cpp:67:50: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘while’
67 | while (*s != '\0') pc(*s), s ++; return;
| ^~~~~~
Main.cpp: In function ‘void Init()’:
Main.cpp:92:25: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
92 | if (1ll * i * j > n) break; vis[i * j] = true;
| ^~
Main.cpp:92:53: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
92 | if (1ll * i * j > n) break; vis[i * j] = true;
| ^~~
Main.cpp: In function ‘std::pair<long long int, std::vector<int> > Get_Phi(int, int)’:
Main.cpp:125:18: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
125 | while (p < fa.size() && q < fb.size()) {
| ~~^~~~~~~~~~~
Main.cpp:125:35: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
125 | while (p < fa.size() && q < fb.size()) {
| ~~^~~~~~~~~~~
Main.cpp:140:18: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
140 | while (p < fa.size()) {
| ~~^~~~~~~~~~~
Main.cpp:147:18: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<std::pair<int, int> >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
147 | while (q < fb.size()) {
| ~~^~~~~~~~~~~
Main.cpp: In function ‘int Solve(int, int, int)’:
Main.cpp:160:9: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
160 | if (!A) return 1; if (A == 1) return __gcd(N, B);
| ^~
Main.cpp:160:27: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
160 | if (!A) return 1; if (A == 1) return __gcd(N, B);
| ^~
Main.cpp:162:36: warning: unused variable ‘g’ [-Wunused-variable]
162 | int d = Gcd(N, A), g;
| ^
Main.cpp: In lambda function:
Main.cpp:172:17: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
172 | if (his.count(n)) return; his[n] = 1;
| ^~
Main.cpp:172:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘if’
172 | if (his.count(n)) return; his[n] = 1;
| ^~~
Judge Result
Set Name
Sample
All
Score / Max Score
0 / 0
1000 / 1000
Status
Set Name
Test Cases
Sample
00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt
All
00-sample-001.txt, 00-sample-002.txt, 00-sample-003.txt, 01-handmade-001.txt, 01-handmade-002.txt, 01-handmade-003.txt, 01-handmade-004.txt, 01-handmade-005.txt, 01-handmade-006.txt, 01-handmade-007.txt, 02-small-001.txt, 02-small-002.txt, 02-small-003.txt, 02-small-004.txt, 02-small-005.txt, 02-small-006.txt, 02-small-007.txt, 02-small-008.txt, 02-small-009.txt, 02-small-010.txt, 02-small-011.txt, 02-small-012.txt, 02-small-013.txt, 02-small-014.txt, 02-small-015.txt, 02-small-016.txt, 02-small-017.txt, 02-small-018.txt, 02-small-019.txt, 02-small-020.txt, 03-random-001.txt, 03-random-002.txt, 03-random-003.txt, 03-random-004.txt, 03-random-005.txt, 03-random-006.txt, 03-random-007.txt, 03-random-008.txt, 03-random-009.txt, 03-random-010.txt, 03-random-011.txt, 03-random-012.txt, 03-random-013.txt, 03-random-014.txt, 03-random-015.txt, 03-random-016.txt, 03-random-017.txt, 03-random-018.txt, 03-random-019.txt, 03-random-020.txt, 04-large-001.txt, 04-large-002.txt, 04-large-003.txt, 04-large-004.txt, 04-large-005.txt, 04-large-006.txt, 04-large-007.txt, 04-large-008.txt, 04-large-009.txt, 04-large-010.txt, 04-large-011.txt, 04-large-012.txt, 04-large-013.txt, 04-large-014.txt, 04-large-015.txt, 04-large-016.txt, 04-large-017.txt, 04-large-018.txt, 04-large-019.txt, 04-large-020.txt, 05-composite-001.txt, 05-composite-002.txt, 05-composite-003.txt, 05-composite-004.txt, 05-composite-005.txt, 05-composite-006.txt, 05-composite-007.txt, 05-composite-008.txt, 05-composite-009.txt, 05-composite-010.txt, 05-composite-011.txt, 05-composite-012.txt, 05-composite-013.txt, 05-composite-014.txt, 05-composite-015.txt, 05-composite-016.txt, 05-composite-017.txt, 05-composite-018.txt, 05-composite-019.txt, 05-composite-020.txt, 05-composite-021.txt, 05-composite-022.txt, 05-composite-023.txt, 05-composite-024.txt, 05-composite-025.txt, 05-composite-026.txt, 05-composite-027.txt, 05-composite-028.txt
Case Name
Status
Exec Time
Memory
00-sample-001.txt
AC
4 ms
3540 KiB
00-sample-002.txt
AC
4 ms
3576 KiB
00-sample-003.txt
AC
4 ms
3544 KiB
01-handmade-001.txt
AC
530 ms
64856 KiB
01-handmade-002.txt
AC
936 ms
64780 KiB
01-handmade-003.txt
AC
634 ms
64776 KiB
01-handmade-004.txt
AC
96 ms
4296 KiB
01-handmade-005.txt
AC
79 ms
4248 KiB
01-handmade-006.txt
AC
1261 ms
35276 KiB
01-handmade-007.txt
AC
370 ms
47876 KiB
02-small-001.txt
AC
4 ms
3520 KiB
02-small-002.txt
AC
4 ms
3536 KiB
02-small-003.txt
AC
4 ms
3580 KiB
02-small-004.txt
AC
4 ms
3536 KiB
02-small-005.txt
AC
4 ms
3544 KiB
02-small-006.txt
AC
5 ms
3512 KiB
02-small-007.txt
AC
4 ms
3516 KiB
02-small-008.txt
AC
4 ms
3604 KiB
02-small-009.txt
AC
4 ms
3524 KiB
02-small-010.txt
AC
5 ms
3600 KiB
02-small-011.txt
AC
4 ms
3492 KiB
02-small-012.txt
AC
4 ms
3496 KiB
02-small-013.txt
AC
4 ms
3520 KiB
02-small-014.txt
AC
4 ms
3500 KiB
02-small-015.txt
AC
4 ms
3524 KiB
02-small-016.txt
AC
4 ms
3516 KiB
02-small-017.txt
AC
4 ms
3420 KiB
02-small-018.txt
AC
5 ms
3576 KiB
02-small-019.txt
AC
5 ms
3668 KiB
02-small-020.txt
AC
5 ms
3428 KiB
03-random-001.txt
AC
537 ms
32460 KiB
03-random-002.txt
AC
934 ms
64664 KiB
03-random-003.txt
AC
623 ms
32604 KiB
03-random-004.txt
AC
530 ms
24060 KiB
03-random-005.txt
AC
691 ms
61348 KiB
03-random-006.txt
AC
842 ms
54384 KiB
03-random-007.txt
AC
714 ms
49012 KiB
03-random-008.txt
AC
513 ms
9244 KiB
03-random-009.txt
AC
552 ms
40260 KiB
03-random-010.txt
AC
710 ms
50040 KiB
03-random-011.txt
AC
425 ms
10672 KiB
03-random-012.txt
AC
528 ms
59964 KiB
03-random-013.txt
AC
875 ms
44128 KiB
03-random-014.txt
AC
592 ms
40196 KiB
03-random-015.txt
AC
692 ms
36316 KiB
03-random-016.txt
AC
682 ms
56296 KiB
03-random-017.txt
AC
581 ms
34552 KiB
03-random-018.txt
AC
548 ms
14832 KiB
03-random-019.txt
AC
366 ms
23644 KiB
03-random-020.txt
AC
915 ms
48116 KiB
04-large-001.txt
AC
711 ms
64800 KiB
04-large-002.txt
AC
970 ms
64844 KiB
04-large-003.txt
AC
589 ms
64708 KiB
04-large-004.txt
AC
722 ms
64792 KiB
04-large-005.txt
AC
783 ms
64772 KiB
04-large-006.txt
AC
667 ms
64780 KiB
04-large-007.txt
AC
974 ms
64864 KiB
04-large-008.txt
AC
711 ms
64824 KiB
04-large-009.txt
AC
890 ms
64860 KiB
04-large-010.txt
AC
920 ms
64948 KiB
04-large-011.txt
AC
759 ms
64844 KiB
04-large-012.txt
AC
682 ms
64856 KiB
04-large-013.txt
AC
870 ms
64772 KiB
04-large-014.txt
AC
690 ms
64936 KiB
04-large-015.txt
AC
747 ms
64796 KiB
04-large-016.txt
AC
743 ms
64948 KiB
04-large-017.txt
AC
558 ms
64840 KiB
04-large-018.txt
AC
584 ms
64836 KiB
04-large-019.txt
AC
704 ms
64952 KiB
04-large-020.txt
AC
584 ms
64880 KiB
05-composite-001.txt
AC
364 ms
6144 KiB
05-composite-002.txt
AC
674 ms
6168 KiB
05-composite-003.txt
AC
435 ms
6168 KiB
05-composite-004.txt
AC
236 ms
6312 KiB
05-composite-005.txt
AC
470 ms
9420 KiB
05-composite-006.txt
AC
887 ms
9420 KiB
05-composite-007.txt
AC
561 ms
9500 KiB
05-composite-008.txt
AC
296 ms
9500 KiB
05-composite-009.txt
AC
1264 ms
35220 KiB
05-composite-010.txt
AC
2045 ms
35244 KiB
05-composite-011.txt
AC
1521 ms
35372 KiB
05-composite-012.txt
AC
1073 ms
35252 KiB
05-composite-013.txt
AC
297 ms
36048 KiB
05-composite-014.txt
AC
469 ms
36208 KiB
05-composite-015.txt
AC
394 ms
36204 KiB
05-composite-016.txt
AC
142 ms
36092 KiB
05-composite-017.txt
AC
428 ms
36524 KiB
05-composite-018.txt
AC
562 ms
36404 KiB
05-composite-019.txt
AC
587 ms
36496 KiB
05-composite-020.txt
AC
132 ms
36400 KiB
05-composite-021.txt
AC
1270 ms
47848 KiB
05-composite-022.txt
AC
2523 ms
47916 KiB
05-composite-023.txt
AC
1220 ms
47908 KiB
05-composite-024.txt
AC
941 ms
47904 KiB
05-composite-025.txt
AC
576 ms
55040 KiB
05-composite-026.txt
AC
1002 ms
55084 KiB
05-composite-027.txt
AC
638 ms
55116 KiB
05-composite-028.txt
AC
335 ms
55100 KiB