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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<queue> #include<set> #include<map> using namespace std; typedef double db; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; #define fi first #define se second #define pb push_back #define mp make_pair #define sz(x) ((int)(x).size()) #define all(x) begin(x),end(x) #define rep(i,l,r) for(int i=(l);i<(r);++i) #define per(i,l,r) for(int i=(r)-1;i>=(l);--i) #define dd(x) cout << #x << "=" << x << ", " #define de(x) cout << #x << "=" << x << endl
const int P = 9973; const int M = 10; const int N = 31625; int n, m, k; vector<int> prime; bool isprime[N];
struct Matrix { int a[M][M]; void init(int x) { rep(i, 0, m) rep(j, 0, m) a[i][j] = i == j ? x : 0; } void fill(int x) { rep(i, 0, m) rep(j, 0, m) a[i][j] = x; } int trace() { int ret = 0; rep(i, 0, m) ret += a[i][i]; return ret % P; } Matrix operator*(const Matrix &mat) const { Matrix r; r.init(0); rep(i, 0, m) rep(j, 0, m) { rep(k, 0, m) r.a[i][j] += a[i][k] * mat.a[k][j]; r.a[i][j] %= P; } return r; } Matrix operator^(int n) { Matrix r, a = *this; r.init(1); while (n > 0) { if (n & 1) r = r * a; a = a * a, n >>= 1; } return r; } };
inline void inc(int &x, int y) { if ((x += y) >= P) x -= P; }
void initPrime() { memset(isprime, 1, sizeof(isprime)); isprime[0] = isprime[1] = false; rep(i, 2, N) { if (isprime[i]) prime.push_back(i); for (int j = 0; i * prime[j] < N; ++j) { isprime[i * prime[j]] = false; if (i % prime[j] == 0) break; } } }
int phi(int n) { int ret = n; rep(i, 0, sz(prime)) { if (n < prime[i]) break; if (n % prime[i] == 0) { ret -= ret / prime[i]; while (n % prime[i] == 0) n /= prime[i]; } } if (n > 1) ret -= ret / n; return ret; }
int f(int n, int d, Matrix &a) { int fd = (a ^ d).trace(); return phi(n / d) % P * fd % P; }
void exgcd(int a, int b, int &x, int &y) { if (!b) x = 1, y = 0; else { exgcd(b, a % b, y, x); y -= a / b * x; } }
int inv(int n) { int x, y; exgcd(n, P, x, y); x = (x % P + P) % P; return x; }
int main() { initPrime();
int cases; scanf("%d", &cases); while (cases-- > 0) { scanf("%d%d%d", &n, &m, &k); Matrix a; a.fill(1); rep(_k, 0, k) { int x, y; scanf("%d%d", &x, &y); --x, --y; a.a[x][y] = a.a[y][x] = 0; } int ans = 0; for (int d = 1; d * d <= n; ++d) { if (n % d) continue; inc(ans, f(n, d, a)); if (d * d != n) inc(ans, f(n, n / d, a)); } ans = ans * inv(n) % P; printf("%d\n", ans); } return 0; }
|