BestCoder #3 (HDU 4907 HDU 4908 HDU 4909 HDU 4910)
来源:互联网 发布:杀人案 知乎 编辑:程序博客网 时间:2024/06/05 23:12
HDU 4908 A - Task schedule
#include <bits/stdc++.h>using namespace std;const int MAXN = 200000 + 7;int a[MAXN], vis[MAXN];int main() { int T; scanf("%d", &T); while(T--) { int n, m; scanf("%d %d", &n, &m); int t, maxx = 0; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; ++i) { scanf("%d", &t); vis[t] = 1; maxx = max(maxx, t); } int idx = 0, tmp = 1; for(int i = 1; i <= maxx + 1; ++i) { if(vis[i] == 0) { a[idx++] = tmp; } else { tmp = i + 1; } } int q; for(int i = 1; i <= m; ++i) { scanf("%d", &q); int ans = a[lower_bound(a, a + idx, q) - a]; if((q < ans && vis[q] == 0) || q >= a[idx - 1]) ans = q; printf("%d\n", ans); } } return 0;}
HDU 4908 B - BestCoder Sequence
#include <bits/stdc++.h>using namespace std;const int MAXN = 40000 + 7;int a[MAXN];int main() { int n, m; while(scanf("%d %d", &n, &m) != EOF) { int ans = 0, idx = -1; int l, g; for(int i = 0; i < n; ++i) { scanf("%d", a + i); if(a[i] == m) { idx = i; } } map<int, int> mm; l = g = 0; for(int i = idx - 1; i >= 0; --i) { if(a[i] < m) l++; if(a[i] > m) g++; if(l == g) ans++; mm[l-g]++; } l = 0, g = 0; for(int i = idx + 1; i < n; ++i) { if(a[i] < m) l++; if(a[i] > m) g++; if(l == g) ans++; if(mm[g-l] != 0) ans += mm[g-l]; } printf("%d\n", ans + 1); } return 0;}
C - String
#include <bits/stdc++.h>using namespace std;const int MAXN = 20000 + 7;char str[MAXN];int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", str); int n = strlen(str), idx = -1; for(int i = 0; i < n; ++i) { if(str[i] == '?') idx = i; } if(idx == -1) { int state = 0, ans = 0; map<int, int> m; m[0] = 1; for(int i = 0; i < n; ++i) { state ^= (1 << (str[i] - 'a')); ans += m[state]; m[state]++; } printf("%d\n", ans); } else { map<int, int> mp; int ans = 0, state = 0; mp[state]++; for(int i = idx + 1; i < n; ++i) { state ^= (1 << (str[i] - 'a')); if(mp.count(state)) { ans += mp[state]; } mp[state]++; } mp.clear(); state = 0; mp[state]++; for(int i = 0; i < idx; ++i) { state ^= (1 << (str[i] - 'a')); if(mp.count(state)) { ans += mp[state]; } mp[state]++; } for(int i = 0; i <= 26; ++i) { int tmp = state; if(i != 26) { tmp ^= (1 << i); if(mp.count(tmp)) { ans += mp[tmp]; } } else { if(mp.count(tmp)) { ans += mp[tmp]; } } for(int j = idx + 1; j < n; ++j) { tmp ^= (1 << (str[j] - 'a')); if(mp.count(tmp)) { ans += mp[tmp]; } } } printf("%d\n", ans); } } return 0;}
HDU 4910
D - Problem about GCD
#include <bits/stdc++.h>using namespace std;const int MAXN = 1000000 + 7;bool vis[MAXN];long long prime[MAXN];int getPrime() { memset(vis, true, sizeof(vis)); memset(prime, 0, sizeof(prime)); int idx = 0; for(long long i = 2; i <= 1000000; ++i) { if(vis[i]) prime[++idx] = i; for(long long j = 1; (j <= idx) && (i * prime[j] <= 1000000); ++j) { vis[i * prime[j]] = false; if(i % prime[j] == 0) break; } } return idx;}long long quickMul(long long a, long long b, long long mod) { long long ret = 0; a %= mod; while(b) { if(b & 1) ret = (ret + a) % mod; a = (a + a) % mod; b >>= 1; } return ret;}long long quickPow(long long a, long long p, long long mod) { long long ans = 1; a %= mod; while(p) { if(p & 1) ans = quickMul(ans, a, mod); a = quickMul(a, a, mod); p >>= 1; } return ans;}bool witness(long long a, long long n) {// let t and u be such that >= 1, u is odd , and n - 1 = u * 2^t long long t = 0, u = n - 1; while(u % 2 == 0) { u /= 2; t++; } //cerr << "n = " << n << " a = " << a << " u = " << u << " t = " << t << endl; long long x_pre = quickPow(a, u, n), x_now; for(int i = 0; i < t; ++i) { x_now = quickMul(x_pre, x_pre, n); if(x_now == 1 && x_pre != 1 && x_pre != n - 1) { return true; } // cerr << " x_pre = " << x_pre << " x_now = " << x_now << endl; x_pre = x_now; } if(x_now != 1) return true; return false;}bool miller_rabin(long long n, long long s) {// return true iff n is a prime srand(time(0)); for(int i = 0; i < s; ++i) { long long tmp = ceil((double)1.0 * rand() / RAND_MAX * (n - 1)); if(witness(tmp, n)) return false; } return true;}bool pesudoPrime(long long n, int s) { if(n < 2) return false; srand(time(0)); for(int i = 0; i < s; ++i) { long long tmp = (long long)((double)rand() / RAND_MAX * n + 0.5); if(quickPow(tmp, n - 1, n) != 1) return false; } return true;}bool check(long long n, int cnt) { if(n % 2 == 0) n /= 2; for(int i = 2; i <= cnt; ++i) { // starting from prime 3 long long tmp = n; while(tmp % prime[i] == 0) tmp /= prime[i]; if(tmp == 1) return true; } if(n <= 1000000LL) return false; if(miller_rabin(n, 20)) return true; long long x = sqrt(n + 0.0); while(x * x < n) x++; if(x * x != n) return false; if(miller_rabin(x, 20)) return true; return false;}int main() { int cnt = getPrime(); long long n; while(scanf("%lld", &n) != EOF) { if(n == -1) break; if(n == 1 || n == 2 || n == 4 || check(n, cnt)) printf("%lld\n", n - 1); else printf("1\n"); } return 0;}
阅读全文
0 0
- BestCoder #3 (HDU 4907 HDU 4908 HDU 4909 HDU 4910)
- hdu 4908 BestCoder Sequence
- hdu 4908 BestCoder Sequence
- HDU 4908 BestCoder Sequence
- hdu 4908 BestCoder Sequence
- HDU 4908 BestCoder Sequence
- hdu 4908 BestCoder Sequence
- hdu 4908 BestCoder Sequence
- HDU 4908 BestCoder Sequence
- HDU 4908BestCoder Sequence
- hdu 4910 String && BestCoder Round #3
- BestCoder round#3 1001 && Hdu 4907
- HDU 4907 Task schedule(BestCoder Round #3 )
- 【HDU】4908 BestCoder Sequence 预处理
- hdu 4908 BestCoder Sequence(计数)
- hdu 4908 BestCoder Sequence【DP】
- HDU 4908 BestCoder Sequence (hash)
- hdu 4908 BestCoder Sequence 策略
- CentOS切换yum源
- MongoCollection 与 iterator_to_array
- UML基本概念--类图关系 依赖、关联、泛化、实现
- 1007. 素数对猜想 (20)
- flume实时收集日志到kafka
- BestCoder #3 (HDU 4907 HDU 4908 HDU 4909 HDU 4910)
- GlobalMapper破解汉化版下载安装图文教程
- ViewAnimator详解之滚动显示TextView
- 安装配置Rancher管理docker
- 【Linux】中的shell脚本语法篇之函数和数组
- Android view组件
- 循环神经网络(RNN)练习:比特币市场的分析与预测
- 设备驱动,字符设备驱动、(总线)设备驱动模型、sysfs文件系统、平台设备驱动
- 详解大端模式和小端模式