Codeforces Round #258 (Div. 2 ),

Source: Internet
Author: User

Codeforces Round #258 (Div. 2 ),
Codeforces Round #258 (Div. 2)

Question Link

A: The number of intersections is min (n, m), so you can directly judge the parity of min (n, m ).

B: open one more array, save the rearranged sequence, push the two sequences from left to right and from right to left to different positions, and then on different segments, only when the beginning and end are different

C: drawing a picture on the paper can easily be discussed in four cases: a> B> c, a <B <c, a> B <c, a <B> c
In four cases, one condition is met.

D: It is easy to see that, as long as the beginning and end of the string are the same, it is the back-to-text. For the length of an odd number, as long as the first and end of the string are both odd or even, for the even length, you can select different lengths for the first and last mail, so you can first pre-process the number of a at the surprising number, the number of a at the even number, and the number of B at the odd number, the number of B at the even position. The odd length is C (ODD a, 2) + C (odd B, 2) + C (even a, 2) + C (even B, 2) + len (the length of 1 is also the return), the length of the even number is odd a even a + even a odd B

E: using the volume rejection principle, a large number of groups are required in the middle. However, the m of C (n, m) in the formula provided will not exceed 20, therefore, each value can be calculated directly using the reverse element. This is the case if n components are known to be s and there is no limit to the number of values, use the partition method to find the condition C (s + n-1, n-1), but this part contains more than, then use the binary to list which is more, the implementation of s minus f (I) + 1 to ensure that this location is exceeded. After this part is subtracted, more Subtraction is added, this satisfies the formula for the concept of the refresh principle. If the number is an odd number, it is subtracted. If the number is an even number, it is added back.

Code:

A:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n, m;int main() {    scanf("%d%d", &n, &m);    if (n > m) swap(n, m);    printf("%s\n", n % 2 ? "Akshat" : "Malvika");    return 0;}

B:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 100005;int n, a[N], b[N];int main() {    scanf("%d", &n);    for (int i = 0; i < n; i++) {    scanf("%d", &a[i]);    b[i] = a[i];    }    sort(a, a + n);    int l = 0, r = n - 1;    while (l <= n - 1 && a[l] == b[l]) {    l++;    }    while (r >= 0 && a[r] == b[r]) {    r--;    }    if (l >= r) {    printf("yes\n");    printf("1 1\n");    }    else {    int flag = 1;    int aa = l, bb = r;    for (int i = l; i <= r; i++) {        if (a[i] != b[r - i + l]) {        flag = 0;        break;        }    }    if (flag) {        printf("yes\n");        printf("%d %d\n", aa + 1, bb + 1);    }    else printf("no\n");    }    return 0;}

C:

#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std;typedef long long ll;ll t, n, k, d1, d2;bool j1(ll n, ll k, ll d1, ll d2) {    if (d1 + d2 + d2 > k) return false;    ll sum = d1 + d2 + d2 + n - k;    if (sum % 3 == 0 && sum / 3 >= d1 + d2) return true;    return false;}bool j2(ll n, ll k, ll d1, ll d2) {    if (d1 + d1 + d2 > k) return false;    ll sum = d1 + d1 + d2 + n - k;    if (sum % 3 == 0 && sum / 3 >= d1 + d2) return true;    return false;}bool j3(ll n, ll k, ll d1, ll d2) {    if (d1 + d2 > k) return false;    ll sum = d1 + d2 + n - k;    ll Max = max(d1, d2);    if (sum % 3 == 0 && sum / 3 >= Max) return true;    return false;}bool j4(ll n, ll k, ll d1, ll d2) {    ll Min = min(d1, d2);    ll Max = max(d1, d2);    ll sum = 2 * abs(d1 - d2) + Min + n - k;    if (2 * abs(d1 - d2) + Min > k) return false;    if (sum % 3 == 0 && sum / 3 >= Max) return true;    return false;}bool judge(ll n, ll k, ll d1, ll d2) {    if (n % 3) return false;    if (j1(n, k, d1, d2)) return true;    if (j2(n, k, d1, d2)) return true;    if (j3(n, k, d1, d2)) return true;    if (j4(n, k, d1, d2)) return true;    return false;}int main() {    scanf("%lld", &t);    while (t--) {    scanf("%lld%lld%lld%lld", &n, &k, &d1, &d2);    if (judge(n, k, d1, d2)) printf("yes\n");    else printf("no\n");    }    return 0;}

D:

#include <cstdio>#include <cstring>const int N = 100005;char str[N];long long ja, jb, oa, ob;long long ansj, anso;int main() {    scanf("%s", str + 1);    int len = strlen(str + 1);    for (int i = 1; i <= len; i++) {    if (i % 2 && str[i] == 'a') ja++;    if (i % 2 && str[i] == 'b') jb++;    if (i % 2 == 0 && str[i] == 'a') oa++;    if (i % 2 == 0 && str[i] == 'b') ob++;    }    ansj = ja * (ja - 1) / 2 + jb * (jb - 1) / 2 + oa * (oa - 1) / 2 + ob * (ob - 1) / 2;    ansj += len;    anso = ja * oa + jb * ob;    printf("%lld %lld\n", anso, ansj);        return 0;}

E:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const long long MOD = 1000000007;const int N = 25;long long n, s, f[N];long long pow(long long x, long long k) {    long long ans = 1;    while (k) {if (k&1) ans = ans * x % MOD;x = x * x % MOD;k >>= 1;    }    return ans;}long long C(long long n, long long m) {    if (m > n) return 0;    m = min(m, n - m);    long long zi = 1, mu = 1;    for (long long i = 0; i < m; i++) {zi = zi * (n - i) % MOD;mu = mu * (i + 1) % MOD;    }    return zi * pow(mu, MOD - 2) % MOD;}long long Lucas(long long n, long long m, long long p) {    if (m == 0) return 1;    return C(n % p, m % p) * Lucas(n / p, m / p, p) % p;}int bitcount(long long x) {    return x == 0 ? 0 : bitcount(x / 2) + (x&1);}int main() {    scanf("%lld%lld", &n, &s);    for (int i = 0; i < n; i++)scanf("%lld", &f[i]);    long long maxs = (1<<n);    long long ans = 0;    for (int i = 0; i < maxs; i++) {long long sum = s;for (int j = 0; j < n; j++) {    if (i&(1<<j)) {sum -= (f[j] + 1);    }    if (sum < 0) break;}if (sum < 0) continue;int tmp = bitcount(i);if (tmp&1) ans -= Lucas(sum + n - 1, n - 1, MOD);else ans += Lucas(sum + n - 1, n - 1, MOD);    }    ans = (ans % MOD + MOD) % MOD;    printf("%lld\n", ans);    return 0;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.