@Dmaxiya
2018-08-17T02:16:35.000000Z
字数 5838
阅读 1372
Codeforces
Contests 链接:Codeforces Round #470 (Div. 1)
过题数:3
排名:315/1183
和 两人在玩一个游戏, 先开始,两人轮流进行,第 次游戏中,对于一个整数 ,选择一个素数 ,然后找到一个最小的整数 ,使得 且 能被 整除,将这个数字作为下一次游戏的新值 继续进行游戏,现在游戏已经进行了两次,得到了 的值,问最小的可能的 的值为多少。
输入为一个整数 ,数据保证 是合数。
输出最小的可能的 的值。
| 输入 |
|---|
| 14 |
| 输出 |
| 6 |
| 提示 |
| 设 ,则其中一种合法的游戏过程如下: 1. 选择 , 的值为 ; 2. 选择 , 的值为 。 |
| 输入 |
|---|
| 20 |
| 输出 |
| 15 |
| 提示 |
| 设 ,则其中一种合法的游戏过程如下: 1. 选择 , 的值为 ; 2. 选择 , 的值为 。 |
| 输入 |
|---|
| 8192 |
| 输出 |
| 8191 |
我们可以根据 的值确定 的合法的取值区间:,其中 为 的最大的质因子,对于 的值,只要选择质数 就可以通过 得到 的值,对于 的值,如果选择质数 ,则只能得到 的值,如果选择其他质数,由于 是最大的质因数,选择其他质数能够改变的增量一定小于 ,所以 。但是最小的 并不能得到最小的 ,因此对于 我们需要枚举所有可能的 的值,来找到最小的 的值。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 1000000 + 100;int cnt, n;bool vis[maxn];int prime[maxn];void Prime() {for(int i = 2; i < maxn; ++i) {if(!vis[i]) {prime[cnt++] = i;}for(int j = 0; j < cnt && i < maxn / prime[j]; ++j) {int k = i * prime[j];vis[k] = true;if(i % prime[j] == 0) {break;}}}}int Find(int x) {int ret;int xx = x;for(int i = 0; i < cnt && prime[i] <= xx / prime[i]; ++i) {while(x % prime[i] == 0) {x /= prime[i];ret = prime[i];}}if(x != 1) {ret = x;}return ret;}int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);Prime();while(scanf("%d", &n) != EOF) {int Start = Find(n);int ans = INT_MAX;for(int i = n - Start + 1; i <= n; ++i) {int tmp = i - Find(i) + 1;if(tmp >= 3) {ans = min(ans, tmp);}}printf("%d\n", ans);}return 0;}
从第 天到第 天每天堆体积为 体积的雪人,第 天的时候每一个已经堆好的雪人会融化 的体积,问在第 天当天所有已经堆好的且没有完全融化的雪人会融化的总的体积。
第一行包含一个整数 ,第 行包含 个整数 ,第 行包含 个整数 。
输出 个整数,第 个整数表示第 天融化的雪人体积。
| 输入 |
|---|
| 3 10 10 5 5 7 2 |
| 输出 |
| 5 12 4 |
| 提示 |
| 第一天堆了一个体积为 的雪人,融化了 的体积,剩下一个体积为 的雪人,第二天堆了一个体积为 的雪人,每个雪人都融化 的体积,第一天的 体积的雪人全都融化了,第二天的雪人融化了 体积,总共融化了 体积,第二天只剩下一个体积为 的雪人,第三天堆了一个体积为 的雪人,第二天雪人剩下的体积和第三天雪人的体积都大于等于第三天融化的体积,第三天总共融化了 体积的雪人。 |
| 输入 |
|---|
| 5 30 25 20 15 10 9 10 12 4 13 |
| 输出 |
| 9 20 35 11 25 |
假设第 天堆起来的雪人体积为无穷大,那么到第 天的时候这个雪人融化的体积就为 ,第 天刚堆起来的雪人没有受到前 天融化的影响,但是可以假设这个雪人是从第 天堆起来的,第 天的时候融化了 的体积,导致它第 天的体积为 ,因此可以认为这个雪人在第 天堆起来的时候的体积为 ,我们将到第 天的所有雪人的体积都转化为 后放入 中,到第 天完全融化的雪人就是 的雪人,将这些雪人在第 天融化的体积()计算出来,然后从 中删去它们,再计算第 天能融化体积 的雪人的个数 (可以 维护),于是第 天融化的雪人体积就为 ,其中 。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 100000 + 100;int n, cnt;LL mp_cnt;map<LL, int> mp;map<LL, int>::iterator it, itt;LL v[maxn], t[maxn];map<LL, int>::iterator del[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {mp_cnt = 0;for(int i = 1; i <= n; ++i) {scanf("%I64d", &v[i]);}for(int i = 1; i <= n; ++i) {scanf("%I64d", &t[i]);t[i] += t[i - 1];}mp.clear();LL ans;for(int i = 1; i <= n; ++i) {ans = 0;cnt = 0;v[i] += t[i - 1];++mp[v[i]];++mp_cnt;it = mp.upper_bound(t[i]);for(itt = mp.begin(); itt != it; ++itt) {ans += ((itt->first) - t[i - 1]) * (itt->second);del[cnt++] = itt;mp_cnt -= itt->second;}for(int j = 0; j < cnt; ++j) {mp.erase(del[j]);}ans += mp_cnt * (t[i] - t[i - 1]);if(i != 1) {printf(" ");}printf("%I64d", ans);}printf("\n");}return 0;}
有两个长度为 的序列 和 ,对于每一个 ,在 序列中找到一个数字与其异或,成为新序列的第 个元素 ,然后从 序列中删去被选中的数字,求能够构成的新序列中字典序最小的序列。
第一行包含一个整数 ,第二行为 个整数 ,第三行为 个整数 。
输出 个整数,第 个整数表示所求新序列的第 项。
| 输入 |
|---|
| 3 8 4 13 17 2 7 |
| 输出 |
| 10 3 28 |
| 提示 |
| ,,。对于其他可能的答案:,都比 的字典序大。 |
| 输入 |
|---|
| 5 12 7 87 22 11 18 39 9 12 16 |
| 输出 |
| 0 14 69 6 44 |
| 输入 |
|---|
| 10 331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951 226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667 |
| 输出 |
| 128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284 |
将所有 序列中的数字建一棵字典树,从 到 对于每一个 ,都取一个 序列中与 异或值最小的,然后将这个数字在字典树中出现的次数 。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxm = 300000 + 100;const int maxn = 300000 * 32 + 100;const int Size = 2;struct Trie {int root, cnt;int val[maxn], tree[maxn][Size];int creat() {++cnt;memset(tree[cnt], 0, sizeof(tree[cnt]));val[cnt] = 0;return cnt;}void Init() {cnt = 0;root = creat();}int id(int x, int dig) {return ((x >> (31 - dig)) & 1);}void add(int x) {int pos = root;for(int i = 0; i < 32; ++i) {int w = id(x, i);if(tree[pos][w] == 0) {tree[pos][w] = creat();}pos = tree[pos][w];++val[pos];}}int query(int x) {int ret = 0;int pos = root;for(int i = 0; i < 32; ++i) {int w = id(x, i);if(tree[pos][w] == 0 || val[tree[pos][w]] == 0) {w = !w;ret |= (1 << (31 - i));}pos = tree[pos][w];--val[pos];}return ret;}};Trie t;int n, x;int num[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {t.Init();for(int i = 0; i < n; ++i) {scanf("%d", &num[i]);}for(int i = 0; i < n; ++i) {scanf("%d", &x);t.add(x);}for(int i = 0; i < n; ++i) {if(i != 0) {printf(" ");}printf("%d", t.query(num[i]));}printf("\n");}return 0;}