A. Distinct Digits
Description
Solution
B. Filling the Grid
Description
Solution
模拟题意,找出没有被固定的方格个数,快速幂。

1 #include <algorithm>
2 #include <cctype>
3 #include <cmath>
4 #include <cstdio>
5 #include <cstdlib>
6 #include <cstring>
7 #include <iostream>
8 #include <map>
9 #include <numeric>
10 #include <queue>
11 #include <set>
12 #include <stack>
13 #if __cplusplus >= 201103L
14 #include <unordered_map>
15 #include <unordered_set>
16 #endif
17 #include <vector>
18 #define lson rt << 1, l, mid
19 #define rson rt << 1 | 1, mid + 1, r
20 #define LONG_LONG_MAX 9223372036854775807LL
21 #define pblank putchar(' ')
22 #define ll LL
23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
24 using namespace std;
25 typedef long long ll;
26 typedef long double ld;
27 typedef unsigned long long ull;
28 typedef pair<int, int> P;
29 int n, m, k;
30 const int maxn = 1e5 + 10;
31 const ll mod = 1000000007;
32 template <class T>
33 inline T read()
34 {
35 int f = 1;
36 T ret = 0;
37 char ch = getchar();
38 while (!isdigit(ch))
39 {
40 if (ch == '-')
41 f = -1;
42 ch = getchar();
43 }
44 while (isdigit(ch))
45 {
46 ret = (ret << 1) + (ret << 3) + ch - '0';
47 ch = getchar();
48 }
49 ret *= f;
50 return ret;
51 }
52 template <class T>
53 inline void write(T n)
54 {
55 if (n < 0)
56 {
57 putchar('-');
58 n = -n;
59 }
60 if (n >= 10)
61 {
62 write(n / 10);
63 }
64 putchar(n % 10 + '0');
65 }
66 template <class T>
67 inline void writeln(const T &n)
68 {
69 write(n);
70 puts("");
71 }
72 template <typename T>
73 void _write(const T &t)
74 {
75 write(t);
76 }
77 template <typename T, typename... Args>
78 void _write(const T &t, Args... args)
79 {
80 write(t), pblank;
81 _write(args...);
82 }
83 template <typename T, typename... Args>
84 inline void write_line(const T &t, const Args &... data)
85 {
86 _write(t, data...);
87 }
88 int h, w;
89 int r[maxn], c[maxn];
90 int mp[1010][1010];
91 int main(int argc, char const *argv[])
92 {
93 #ifndef ONLINE_JUDGE
94 freopen("in.txt", "r", stdin);
95 // freopen("out.txt","w", stdout);
96 #endif
97 h = read<int>(), w = read<int>();
98 for (int i = 1; i <= h; i++)
99 r[i] = read<int>();
100 for (int i = 1; i <= w; i++)
101 c[i] = read<int>();
102 for (int i = 1; i <= h; i++)
103 if (r[i])
104 {
105 for (int j = 1; j <= r[i]; j++)
106 mp[i][j] = 1;
107 mp[i][r[i] + 1] = -1;
108 }
109 else
110 mp[i][1] = -1;
111 int f = 1;
112 for (int i = 1; i <= w; i++)
113 if (c[i])
114 {
115 for (int j = 1; j <= c[i]; j++)
116 {
117 if (mp[j][i] == -1)
118 {
119 f = 0;
120 break;
121 }
122 mp[j][i] = 1;
123 }
124 if (mp[c[i] + 1][i] == 1)
125 {
126 f = 0;
127 break;
128 }
129 else
130 mp[c[i] + 1][i] = -1;
131 }
132 else
133 {
134 if (mp[1][i] == 1)
135 {
136 f = 0;
137 break;
138 }
139 else
140 mp[1][i] = -1;
141 }
142 ll res = 0;
143 for (int i = 1; i <= h; i++)
144 for (int j = 1; j <= w; j++)
145 if (!mp[i][j])
146 ++res;
147 ll ans = 1;
148 ll base = 2;
149 for (; res; res >>= 1)
150 {
151 if (res & 1)
152 ans = ans * base % mod;
153 base = base * base % mod;
154 }
155 if (f)
156 writeln(ans);
157 else
158 puts("0");
159 return 0;
160 }
C. Primes and Multiplication
Description

Solution
注意到g(x,y)是计算x里y的最大幂次约数。
那么答案可以转换为$n!$里包含的x素因子的最大幂次约数之积。
唯一分解+阶乘素因子分解。
!!!由于n数量级在1e18,其阶乘的素因子指数有可能爆int,注意开ll。(wa了一发)

1 #include <algorithm>
2 #include <numeric>
3 #include <cctype>
4 #include <cmath>
5 #include <cstdio>
6 #include <cstdlib>
7 #include <cstring>
8 #include <iostream>
9 #include <map>
10 #include <queue>
11 #include <set>
12 #include <stack>
13 #if __cplusplus >= 201103L
14 #include <unordered_map>
15 #include <unordered_set>
16 #endif
17 #include <vector>
18 #define lson rt << 1, l, mid
19 #define rson rt << 1 | 1, mid + 1, r
20 #define LONG_LONG_MAX 9223372036854775807LL
21 #define pblank putchar(' ')
22 #define ll LL
23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
24 using namespace std;
25 typedef long long ll;
26 typedef long double ld;
27 typedef unsigned long long ull;
28 typedef pair<int, int> P;
29 ll n, m, k;
30 const int maxn = 1e6 + 10;
31 const ll mod = 1e9 + 7;
32 template <class T>
33 inline T read()
34 {
35 int f = 1;
36 T ret = 0;
37 char ch = getchar();
38 while (!isdigit(ch))
39 {
40 if (ch == '-')
41 f = -1;
42 ch = getchar();
43 }
44 while (isdigit(ch))
45 {
46 ret = (ret << 1) + (ret << 3) + ch - '0';
47 ch = getchar();
48 }
49 ret *= f;
50 return ret;
51 }
52 template <class T>
53 inline void write(T n)
54 {
55 if (n < 0)
56 {
57 putchar('-');
58 n = -n;
59 }
60 if (n >= 10)
61 {
62 write(n / 10);
63 }
64 putchar(n % 10 + '0');
65 }
66 template <class T>
67 inline void writeln(const T &n)
68 {
69 write(n);
70 puts("");
71 }
72 template <typename T>
73 void _write(const T &t)
74 {
75 write(t);
76 }
77 template <typename T, typename... Args>
78 void _write(const T &t, Args... args)
79 {
80 write(t), pblank;
81 _write(args...);
82 }
83 template <typename T, typename... Args>
84 inline void write_line(const T &t, const Args &... data)
85 {
86 _write(t, data...);
87 puts("");
88 }
89 int prime[maxn], vis[maxn], pcnt;
90 void init(){
91 for (int i = 2; i < maxn;i++){
92 if (!vis[i])
93 prime[pcnt++] = i;
94 for (int j = 0; j < pcnt && i * prime[j] < maxn;j++){
95 vis[i * prime[j]] = 1;
96 if (i%prime[j]==0)
97 break;
98 }
99 }
100 }
101 ll fac[maxn], tot;
102 inline ll qpow(ll base,ll n){
103 ll res = 1;
104 while(n){
105 if(n&1)
106 res = res * base % mod;
107 base = base * base % mod;
108 n >>= 1;
109 }
110 return res;
111 }
112 ll f(ll n, int p)
113 {
114 if (n == 0)
115 return 0;
116 return f(n / p, p) + n / p;
117 }
118 int main(int argc, char const *argv[])
119 {
120 #ifndef ONLINE_JUDGE
121 freopen("in.txt","r", stdin);
122 // freopen("out.txt","w", stdout);
123 #endif
124 init();
125 ll x = read<ll>();
126 n = read<ll>();
127 for (int i = 0; i < pcnt;i++){
128 if (x%prime[i]==0){
129 fac[tot++] = prime[i];
130 while(x%prime[i]==0)
131 x /= prime[i];
132 }
133 }
134 if (x!=1)
135 fac[tot++] = x;
136 ll res = 1;
137 for (int i = 0; i < tot;i++){
138 ll t = f(n, fac[i]);
139 res = res * qpow(fac[i], t) % mod;
140 }
141 writeln(res);
142 return 0;
143 }
D. Complete Tripartite
Description

给出一个可能不连通的无自环无向图,问是否能划分出三个点集。
任意两个点集应该满足每一个顶点到另一个集合的任意点有边,自己点集内任意两点无边相连。
Solution
自己思路补全,看了题解才会。
首先想到dfs一遍判断图是否连通,不连通直接输出-1。
首先将所有点染色为1。
遍历所有染色1的点的邻接点v,如果v的颜色依然为1,则将其标为2。
遍历所有染色2的点的邻接点v,如果v的颜色依然为2,则将其标为3。
做完以上步骤,可以保证三个点集内部点不互相连。
接下来使用点的度数判断是否满足点集间互相有边。
再者是判断是否有一个集合为空。
判断完输出结果。

1 #include <algorithm>
2 #include <cctype>
3 #include <cmath>
4 #include <cstdio>
5 #include <cstdlib>
6 #include <cstring>
7 #include <iostream>
8 #include <map>
9 #include <numeric>
10 #include <queue>
11 #include <set>
12 #include <stack>
13 #if __cplusplus >= 201103L
14 #include <unordered_map>
15 #include <unordered_set>
16 #endif
17 #include <vector>
18 #define lson rt << 1, l, mid
19 #define rson rt << 1 | 1, mid + 1, r
20 #define LONG_LONG_MAX 9223372036854775807LL
21 #define pblank putchar(' ')
22 #define ll LL
23 #define fastIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
24 using namespace std;
25 typedef long long ll;
26 typedef long double ld;
27 typedef unsigned long long ull;
28 typedef pair<int, int> P;
29 int n, m, k;
30 const int maxn = 1e5 + 10;
31 template <class T>
32 inline T read()
33 {
34 int f = 1;
35 T ret = 0;
36 char ch = getchar();
37 while (!isdigit(ch))
38 {
39 if (ch == '-')
40 f = -1;
41 ch = getchar();
42 }
43 while (isdigit(ch))
44 {
45 ret = (ret << 1) + (ret << 3) + ch - '0';
46 ch = getchar();
47 }
48 ret *= f;
49 return ret;
50 }
51 template <class T>
52 inline void write(T n)
53 {
54 if (n < 0)
55 {
56 putchar('-');
57 n = -n;
58 }
59 if (n >= 10)
60 {
61 write(n / 10);
62 }
63 putchar(n % 10 + '0');
64 }
65 template <class T>
66 inline void writeln(const T &n)
67 {
68 write(n);
69 puts("");
70 }
71 template <typename T>
72 void _write(const T &t)
73 {
74 write(t);
75 }
76 template <typename T, typename... Args>
77 void _write(const T &t, Args... args)
78 {
79 write(t), pblank;
80 _write(args...);
81 }
82 template <typename T, typename... Args>
83 inline void write_line(const T &t, const Args &... data)
84 {
85 _write(t, data...);
86 }
87 int col[maxn], deg[maxn];
88 vector<int> g[maxn];
89 void dfs(int u, int p)
90 {
91 col[u] = 1;
92 int sz = g[u].size();
93 for (int i = 0; i < sz; i++)
94 {
95 int v = g[u][i];
96 if (col[v])
97 continue;
98 dfs(v, u);
99 }
100 }
101 int main(int argc, char const *argv[])
102 {
103 #ifndef ONLINE_JUDGE
104 freopen("in.txt", "r", stdin);
105 // freopen("out.txt","w", stdout);
106 #endif
107 n = read<int>(), m = read<int>();
108 for (int i = 0; i < m; i++)
109 {
110 int x = read<int>(), y = read<int>();
111 g[x].emplace_back(y), g[y].emplace_back(x);
112 deg[x]++, deg[y]++;
113 }
114 dfs(1, 0);
115 int p = accumulate(col + 1, col + 1 + n, 0);
116 if (p != n)
117 {
118 puts("-1");
119 return 0;
120 }
121 for (int i = 1; i <= n; i++)
122 {
123 if (col[i] == 1)
124 {
125 int sz = g[i].size();
126 for (int j = 0; j < sz; j++)
127 {
128 int k = g[i][j];
129 if (col[k] == 1)
130 col[k] = 2;
131 }
132 }
133 }
134 for (int i = 1; i <= n; i++)
135 {
136 if (col[i] == 2)
137 {
138 int sz = g[i].size();
139 for (int j = 0; j < sz; j++)
140 {
141 int k = g[i][j];
142 if (col[k] == 2)
143 col[k] = 3;
144 }
145 }
146 }
147 int s[4] = {0};
148 for (int i = 1; i <= n; i++)
149 ++s[col[i]];
150 int f = 1;
151 if (!s[1]||!s[2]||!s[3])
152 f = 0;
153 for (int i = 1; i <= n&&f;i++){
154 int cur = 0;
155 for (int j = 1; j <= 3;j++)
156 if (j!=col[i])
157 cur += s[j];
158 if (cur!=deg[i])
159 f = 0;
160 }
161 if (f)
162 for (int i = 1;i<=n;i++)
163 write(col[i]), pblank;
164 else
165 puts("-1");
166 return 0;
167 }
