codeforces #603 div2 ABCD
A. Sweet Problem
Description
Solution
瞎搞的规律
B. PIN Codes
Description
给出n个4位数字密码,每次操作可以改变一个密码中的一位数字。
求最少的操作次数使得n个密码不同。
Solution
hash一下序列乱搞,代码里的while应该可以去掉,扫一遍就行。
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 puts(""); 87 } 88 vector<int> g[100]; 89 inline int _hash(vector<int> &t) 90 { 91 int p = 1; 92 for (int i = 0; i < 4; i++) 93 p = (p * 2017 + t[i]) % 10007; 94 return p; 95 } 96 inline int isok() 97 { 98 unordered_set<int> s; 99 for (int i = 0; i < n; i++) 100 { 101 int p = _hash(g[i]); 102 if (s.count(p)) 103 return 0; 104 s.emplace(p); 105 } 106 return 1; 107 } 108 int main(int argc, char const *argv[]) 109 { 110 #ifndef ONLINE_JUDGE 111 freopen("in.txt", "r", stdin); 112 // freopen("out.txt", "w", stdout); 113 #endif 114 int t = read<int>(); 115 while (t--) 116 { 117 n = read<int>(); 118 for (int i = 0; i < n; i++) 119 g[i].clear(); 120 for (int i = 0; i < n; i++) 121 { 122 char s[10]; 123 scanf("%s", s); 124 for (int j = 0; j < 4; j++) 125 g[i].emplace_back(s[j] - '0'); 126 } 127 int res = 0; 128 while (!isok()) 129 { 130 unordered_map<int, int> mp; 131 mp.clear(); 132 for (int i = 0; i < n; i++) 133 mp[_hash(g[i])]++; 134 for (int i = 0; i < n; i++) 135 { 136 int p = _hash(g[i]); 137 if (mp[p] == 1) 138 continue; 139 for (int j = 0; j < 4; j++) 140 { 141 int f = 1; 142 for (int k = 0; k < 10 && f; k++) 143 { 144 int tmp = g[i][j]; 145 g[i][j] = k; 146 if (!mp[_hash(g[i])]) 147 { 148 mp[_hash(g[i])] = 1; 149 f = 0; 150 ++res; 151 } 152 } 153 if (!f) 154 break; 155 } 156 mp[p]--; 157 } 158 } 159 writeln(res); 160 for (int i = 0; i < n; i++) 161 { 162 for (int j : g[i]) 163 write(j); 164 puts(""); 165 } 166 } 167 return 0; 168 }
C. Everyone is a Winner!
Description
Solution
数论分块儿裸题,加上一个0即是答案。
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 puts(""); 87 } 88 int main(int argc, char const *argv[]) 89 { 90 #ifndef ONLINE_JUDGE 91 freopen("in.txt", "r", stdin); 92 // freopen("out.txt", "w", stdout); 93 #endif 94 int t = read<int>(); 95 while (t--) 96 { 97 n = read<int>(); 98 vector<int> res; 99 res.clear(); 100 res.emplace_back(0); 101 for (int i = 1, j; i <= n; i = j + 1) 102 { 103 res.emplace_back(n / i); 104 j = n / (n / i); 105 } 106 writeln(res.size()); 107 sort(res.begin(), res.end()); 108 for (int x : res) 109 write(x), pblank; 110 puts(""); 111 } 112 113 return 0; 114 }
D. Secret Passwords
Description
盗贼剽窃到了n个由小写字母构成的密码串。
密码串T,S被认为相同效果的条件:
1,存在一个字符同时出现在T,S中。
2,存在第三个密码串P,满足P=S&&P=T
两者满足其一即可。
问盗贼最少要试多少个密码才能打开保险柜,保险柜仅有一个正确密码。
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 = 2e5 + 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 puts(""); 87 } 88 int fa[maxn]; 89 int ch[26]; 90 inline int getfa(int x) 91 { 92 if (!fa[x]) 93 return x; 94 return fa[x] = getfa(fa[x]); 95 } 96 inline void merge(int x, int y) 97 { 98 x = getfa(x); 99 y = getfa(y); 100 if (x != y) 101 fa[y] = x; 102 } 103 char s[55]; 104 int main(int argc, char const *argv[]) 105 { 106 #ifndef ONLINE_JUDGE 107 freopen("in.txt", "r", stdin); 108 // freopen("out.txt", "w", stdout); 109 #endif 110 n = read<int>(); 111 memset(ch, -1, sizeof(ch)); 112 for (int i = 0; i < n; i++) 113 { 114 scanf("%s", s); 115 int len = strlen(s); 116 for (int j = 0; j < len; j++) 117 { 118 int idx = s[j] - 'a'; 119 if (ch[idx] == -1) 120 ch[idx] = i; 121 else 122 merge(i, ch[idx]); 123 } 124 } 125 int res = 0; 126 for (int i = 0; i < n; i++) 127 if (!fa[i]) 128 ++res; 129 writeln(res); 130 return 0; 131 }