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;
}