voidsol(int k, int a, int b, int c){ if (k == 0) return; if (f[k] == c) { ans = -1; return; } elseif (f[k] == b) { ans += 1 << (k-1); sol(k-1, c, b, a); } else sol(k-1, a, c, b); }
intmain(){ cin >> n; for (int i = 1; i <= n; i++) cin >> f[i]; sol(n, 1, 2, 3); cout << ans << endl; return0; }
bool valid[MN]; int ans[MN] = {0}; int f[MN] = {0};
voidgetPrime(int n){ int tot = 0; memset(valid, true, sizeof(valid)); for (int i = 2; i <= n; i++) { if (valid[i]) ans[++tot] = i; for (int j = 1; ((j <= tot) && (i*ans[j] <= n)); j++) { valid[i*ans[j]] = false; if (i % ans[j] == 0) break; } } }
intmain(){ int n, m; cin >> n >> m; if (m > (n/2)) m = n-m; getPrime(n); for (int i = 0; i < m; i++) { int t = n-i; if (valid[t]) { f[t]++; } else { int j = 0; while (t != 1) { j++; while (t % ans[j] == 0) { f[ans[j]]++; t /= ans[j]; } } } } for (int i = 2; i <= m; i++) { if (valid[i]) { f[i]--; } else { int j = 0, t = i; while (t != 1) { j++; while (t % ans[j] == 0) { f[ans[j]]--; t /= ans[j]; } } } } int ans = 0; for (int i = 2; i <= n; i++) if (f[i]) ans++; cout << ans << endl; return0; }
N(<=10000)点构成一颗树,要求出树的中心。由树的性质可以知道,中心必定是1 or 2个。求树的中心还有树的直径都是很经典的问题,这道题也有很多做法。我用的是两边DFS,任意选则一个点v做一遍DFS,则离v最远的那个点必定是树的直径的一个端点,然后再由得到的端点做一遍DFS,就找到了另一个端点了,记录路径,然后再找到直径的中点就行了。
voiddfs(int x, int pr, int de){ vt[x] = true; f[x] = pr; if (de > l) { l = de; v = x; } for (int i = 0; i < a[x].size(); i++) { int ch = a[x][i]; if (vt[ch]) continue; dfs(ch, x, de+1); } }
intmain(){ cin >> n; for (int i = 2; i <= n; i++) { cin >> k; a[k].push_back(i); a[i].push_back(k); } v = l = 0; memset(vt, false, sizeof(vt)); dfs(1, 0, 1); memset(vt, false, sizeof(vt)); l = 0; dfs(v, 0, 1); for (int i = 1; i < l/2; i++) v = f[v]; if (l % 2) cout << f[v] << endl; else cout << min(v, f[v]) << " " << max(v, f[v]) << endl; return0; }
voidinit(){ c[0][0] = 1; for (int i = 1; i < 32; i++) { c[i][0] = c[i-1][0]; for (int j = 1; j <= i; j++) c[i][j] += c[i-1][j-1] + c[i-1][j]; } }
intcal(int x){ int l = 0, ans = 0; while (x) { dgt[++l] = x % b; x /= b; } int mk = 0; for (int i = l; i > 0; i--) { if (dgt[i] > 1) { ans += c[i][k-mk]; break; } elseif (dgt[i] == 1) { if (i > k-mk) ans += c[i-1][k-mk]; if (++mk > k) break; } if ((i == 1) && (mk == k)) ans++; } return ans; }
intmain(){ cin >> x >> y >> k >> b; init(); cout << cal(y) - cal(x-1) << endl; return0; }