1 /*并查集*/ 2 #define MAXN 10000 3 int pre[MAXN]; 4 int find(int x) { 5 return x == pre[x] ? x : pre[x] = find(pre[x]); 6 } 7 void merge(int x, int y) { 8 x = find(x); 9 y = find(y); 10 if (x != y) { 11 pre[x] = y; 12 } 13 } 14 15 /*快速幂*/ 16 #define MOD 10 17 __int64 quickmul(int x, int n) { 18 __int64 ans = 1; 19 __int64 t = n; 20 while (n) { 21 if (n & 1) { 22 ans = (ans * t) % MOD; 23 } 24 t = t * t % MOD; 25 n >>= 1; 26 } 27 return ans; 28 } 29 30 /*矩阵快速幂*/ 31 #define MOD 10 32 #define MAXN 10 33 34 struct Matrix { 35 int m[MAXN][MAXN]; 36 int r, c; 37 Matrix() { 38 r = c = 0; 39 memset(m, 0, sizeof(m)); 40 } 41 }; 42 43 Matrix mul(Matrix m1, Matrix m2, int mod) { 44 Matrix ans = Matrix(); 45 ans.r = m1.r; 46 ans.c = m2.c; 47 for (int i = 1; i <= m1.r; i++) { 48 for (int j = 1; j <= m2.r; j++) { 49 if (m1.m[i][j]) { //优化 50 for (int k = 1; k <= m2.c; k++) { 51 ans.m[i][k] = (ans.m[i][k] + m1.m[i][j] * m2.m[j][k]) % mod; 52 } 53 } 54 } 55 } 56 return ans; 57 } 58 59 Matrix quickmul(Matrix m, int n, int mod) { 60 Matrix ans = Matrix(); 61 for (int i = 1; i <= m.r; i++) { 62 ans.m[i][i] = 1; 63 } 64 ans.r = m.r; 65 ans.c = m.c; 66 while (n) { 67 if (n & 1) { 68 ans = mul(m, ans, mod); 69 } 70 m = mul(m, m, mod); 71 n >>= 1; 72 } 73 return ans; 74 }
1 /*埃氏筛*/ 2 double s_MAXN = sqrt(MAXN); 3 bool pri[MAXN + 1]; 4 5 void prilist() 6 { 7 memset(pri, true, sizeof(pri)); 8 pri[1] = false; 9 for (int i = 2; i <= int(s_MAXN); i++) 10 { 11 if (pri[i] == true) 12 { 13 for (int j = 2; j <= MAXN / i; j++) 14 { 15 pri[i*j] = false; 16 } 17 } 18 } 19 }
时间: 2024-10-04 00:04:48