Codeforces Round #275 (Div. 2),
連結:http://codeforces.com/contest/483
A. Counterexampletime limit per test1 secondmemory limit per test256 megabytes
Your friend has recently learned about coprime numbers. A pair of numbers{a, b} is called coprime if the maximum number that divides both a andb is equal to one.
Your friend often comes up with different statements. He has recently supposed that if the pair(a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime.
You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers(a, b, c), for which the statement is false, and the numbers meet the conditionl ≤ a < b < c ≤ r.
More specifically, you need to find three numbers(a, b, c), such that l ≤ a < b < c ≤ r, pairs (a, b) and (b, c) are coprime, and pair(a, c) is not coprime.
Input
The single line contains two positive space-separated integersl, r (1 ≤ l ≤ r ≤ 1018;r - l ≤ 50).
Output
Print three positive space-separated integers a, b, c — three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order.
If the counterexample does not exist, print the single number -1.
Sample test(s)Input
2 4
Output
2 3 4
Input
10 11
Output
-1
Input
900000000000000009 900000000000000029
Output
900000000000000009 900000000000000010 900000000000000021
Note
In the first sample pair (2, 4) is not coprime and pairs(2, 3) and (3, 4) are.
In the second sample you cannot form a group of three distinct integers, so the answer is -1.
In the third sample it is easy to see that numbers900000000000000009 and 900000000000000021 are divisible by three.
簡單構造:第一個數和第二個數互素,第二個數和第三個數互素,第一個數和第三個數不互素,顯然找偶奇偶序列
#include <cstdio>#define ll long longll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }int main(){ ll a, b; ll ans[3] = {0}; scanf("%I64d %I64d", &a, &b); if(b == a + 1) { printf("-1\n"); return 0; } if(a % 2 == 1) { ans[0] = a + 1; ans[1] = a + 2; } else { ans[0] = a; ans[1] = a + 1; } for(ll i = a + 2; i <= b; i++) { if(gcd(ans[1], i) == 1 && gcd(ans[0], i) != 1) { ans[2] = i; break; } } if(ans[2] == 0) { printf("-1\n"); return 0; } printf("%I64d %I64d %I64d\n", ans[0], ans[1], ans[2]);}
B. Friends and Presentstime limit per test1 secondmemory limit per test256 megabytes
You have two friends. You want to present each of them several positive integers. You want to presentcnt1 numbers to the first friend andcnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime numberx. The second one does not like the numbers that are divisible without remainder by prime numbery. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
Input
The only line contains four positive integers cnt1,cnt2, x, y (1 ≤ cnt1, cnt2 < 109;cnt1 + cnt2 ≤ 109;2 ≤ x < y ≤ 3·104) — the numbers that are described in the statement. It is guaranteed that numbersx, y are prime.
Output
Print a single integer — the answer to the problem.
Sample test(s)Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
Note
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers{2} to the second friend. Note that if you give set{1, 3, 5} to the first friend, then we cannot give any of the numbers1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers{1, 2, 4} to the second friend. Thus, the answer to the problem is4.
構造兩個序列,第一個序列要有cnt1個數且其中不能有x的倍數,第二個序列要有cnt2個數且其中不能有y的倍數。要求求出這兩個序列中的最大數的最小值。
如果答案是ans則ans+1也滿足可以發現問題滿足二分做法的條件,cnt1個數為num - num / x,及1到num中不包含x倍數的數的個數,同理
cnt2個數為num - num / y,還有一點要注意的是必須滿足cnt1 + cnt2 <= num - num / (x * y)因為題目要求兩個集合中的元素不同,又因為x,y必然互素,所以cnt1和cnt2的和必須小於1到num中不包含(x * y)倍數的數的個數,不然兩個集合必然出現重複元素,然後二分num的值便可以得到最終答案。
#include <cstdio>int cal(int a, int b){ return a - a / b;}int main(){ int cnt1, cnt2, x, y; scanf("%d %d %d %d", &cnt1, &cnt2, &x, &y); int l = 1, r = 2e9; while(l < r) { int mid = l + (r - l) / 2; if(cnt1 <= cal(mid, x) && cnt2 <= cal(mid, y) && cnt1 + cnt2 <= cal(mid, x * y)) r = mid; else l = mid + 1; } printf("%d\n", r); }
C. Diverse Permutationtime limit per test1 secondmemory limit per test256 megabytes
Permutationp is an ordered set of integers p1, p2, ..., pn, consisting ofn distinct positive integers not larger thann. We'll denote as n the length of permutation p1, p2, ..., pn.
Your task is to find such permutation p of length n, that the group of numbers|p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn| has exactly k distinct elements.
Input
The single line of the input contains two space-separated positive integersn, k (1 ≤ k < n ≤ 105).
Output
Print n integers forming the permutation. If there are multiple answers, print any of them.
Sample test(s)Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note
By |x| we denote the absolute value of numberx.
一個包含n個數的數列,要求相鄰兩元素差的絕對值的數的個數為k,要求構造這樣的數列。
簡單的構造題,k--並變號判斷即可
#include <cstdio>#include <cstring>int const MAX = 1e5 + 5;int a[MAX];int hash[MAX];int main(){ int n, k; scanf("%d %d", &n, &k); memset(hash, 0, sizeof(hash)); a[1] = 1; hash[a[1]] = 1; int cnt = 1; for(int i = 2; i <= n; i++) { cnt++; a[i] = a[i - 1] + k; hash[a[i]] = 1; if(k > 0) k--; else k++; k *= -1; if(k == 0) break; } for(int i = 1; i <= n; i++) if(!hash[i]) a[++cnt] = i; for(int i = 1; i < n; i++) printf("%d ", a[i]); printf("%d\n", a[n]);}
D. Interesting Arraytime limit per test1 secondmemory limit per test256 megabytes
We'll call an array of n non-negative integersa[1], a[2], ..., a[n]interesting, if it meets m constraints. The i-th of them constraints consists of three integers li, ri, qi (1 ≤ li ≤ ri ≤ n) meaning that value should be equal toqi.
Your task is to find any interesting array ofn elements or state that such array doesn't exist.
Expression x&y means the bitwise AND of numbersx and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".
Input
The first line contains two integers n,m (1 ≤ n ≤ 105,1 ≤ m ≤ 105) — the number of elements in the array and the number of limits.
Each of the next m lines contains three integersli,ri,qi (1 ≤ li ≤ ri ≤ n,0 ≤ qi < 230) describing thei-th limit.
Output
If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line printn integers a[1], a[2], ..., a[n] (0 ≤ a[i] < 230) decribing theinteresting array. If there are multiple answers, print any of them.
If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.
Sample test(s)Input
3 11 3 3
Output
YES3 3 3
Input
3 21 3 31 3 2
Output
NO
題意如題,還是構造數列,線段樹維護+判斷
#include <cstdio>#include <cstring>#include <vector>using namespace std;int const MAX = 110000;struct Node{ int l, r; int val;};Node tree[10 * MAX];Node a[MAX];vector<int> ans;void build(int step, int l, int r){ tree[step].l = l; tree[step].r = r; tree[step].val = 0; if(l == r) return; int mid = (l + r) >> 1; build(step << 1, l, mid); build(((step << 1) + 1), mid+1, r);}void update(int step, int l, int r, int val){ if(tree[step].l == l && tree[step].r == r) { tree[step].val |= val; return; } int mid = (tree[step].l + tree[step].r) >> 1; if(r <= mid) update(step << 1, l, r, val); else if(l > mid) update((step << 1) + 1, l, r, val); else { update(step << 1, l, mid, val); update((step << 1) + 1, mid+1, r, val); }}int query(int step, int l, int r){ if(tree[step].l == l && tree[step].r == r) return tree[step].val; int mid = (tree[step].l + tree[step].r) >> 1; if(r <= mid) return query(step << 1, l, r); if(l > mid) return query((step << 1) + 1, l, r); else return query(step << 1, l, mid) & query((step << 1) + 1, mid+1, r);}void solve(int step){ if(step != 1) tree[step].val |= tree[step >> 1].val; if(tree[step].l == tree[step].r) { ans.push_back(tree[step].val); return ; } solve(step << 1); solve((step << 1) + 1);}int main(){ int n, m; scanf("%d %d", &n, &m); build(1, 1, n); for(int i = 0; i < m; i++) { scanf("%d %d %d", &a[i].l, &a[i].r, &a[i].val); update(1, a[i].l, a[i].r, a[i].val); } bool flag = true; for(int i = 0; i < m; i++) { if(query(1, a[i].l, a[i].r) != a[i].val) { flag = false; break; } } solve(1); if(flag) { printf("YES\n"); for(int i = 0; i < ans.size(); i++) printf("%d%c",ans[i], i == n ? '\n' : ' '); ans.clear(); printf("\n"); } else printf("NO\n");}