Codeforces Round #851 (Div. 2) A~C

\(D\)题之后补吧

A.One and Two


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>
#define rep(i, j, k) for(int i=j;i<=k;i++)

#define int long long


const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 2e5 + 10, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//const int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};
//#define x first
//#define y second
int fac[N];

int qpow(int a, int b) {
    int ans = 1LL, base = a;
    while (b) {
        if (b & 1) ans = (ans * base) % mod;
        base = (base * base) % mod;
        b >>= 1;
    }
    return ans;
}

int C(int n, int k) {
    if (k > n) return 0;
    return (fac[n] * qpow(fac[k], mod - 2) % mod) * qpow(fac[n - k], mod - 2) % mod;
}

int Lucas(int n, int k) {
    if (!k) return 1LL;
    return C(n % mod, k % mod) * Lucas(n / mod, k / mod) % mod;
}

int T;

int a[N];

void solve() {
    int n;
    cin >> n;
    rep(i, 1, n) cin >> a[i];
    int sum = 0;
    rep(i, 1, n) if (a[i] == 2) sum++;
    if (sum == 0) {
        cout << 1 << endl;
        return;
    }
    if (sum & 1) {
        cout << -1 << endl;
        return;
    }
    int sum2 = 0;
    rep(i, 1, n) {
        if (a[i] == 2) sum2++;
        if (sum2 == sum / 2) {
            cout << i << endl;
            return;
        }
    }

}


signed main() {
    IOS;
    cin >> T;
    while (T--)
        solve();
}

B.Sum of Two Numbers

按位拆分,如果\((n)_{10}\)的第\(i\)位为偶数,令\(a_i=b_i=\frac{n_i}{2}\),否则交替令\(a_i、b_i\)相差\(1\)


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>
#define rep(i, j, k) for(int i=j;i<=k;i++)

#define int long long


const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 2e5 + 10, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//const int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};
//#define x first
//#define y second
int fac[N];

int qpow(int a, int b) {
    int ans = 1LL, base = a;
    while (b) {
        if (b & 1) ans = (ans * base) % mod;
        base = (base * base) % mod;
        b >>= 1;
    }
    return ans;
}

int C(int n, int k) {
    if (k > n) return 0;
    return (fac[n] * qpow(fac[k], mod - 2) % mod) * qpow(fac[n - k], mod - 2) % mod;
}

int Lucas(int n, int k) {
    if (!k) return 1LL;
    return C(n % mod, k % mod) * Lucas(n / mod, k / mod) % mod;
}

int T;

int n;

void solve() {
    cin >> n;
    int t = n;
    int num[10 + 5], tot = 0;

    vector<int> a(20, 0), b(20, 0);
    while (t) {
        num[tot++] = t % 10;
        t /= 10;
    }
    bool flag = 0;
    for (int i = 0; i < tot; i++) {
        if (num[i] & 1) {
            if (!flag) {
                a[i] = (num[i] + 1) / 2;
                b[i] = (num[i] - 1) / 2;
                flag = 1;
                continue;
            } else {
                a[i] = (num[i] - 1) / 2;
                b[i] = (num[i] + 1) / 2;
                flag = 0;
                continue;
            }
        } else {
            a[i] = b[i] = num[i] / 2;
        }
    }
    int A = 0, B = 0;
    for (int i = 0; i < tot; i++)
        A += a[i] * pow(10, i), B += b[i] * pow(10, i);
    cout << A << " " << B << endl;
}


signed main() {
    IOS;
    cin >> T;
    while (T--)
        solve();
}

C.Matching Numbers

\(S_i\)是个满足\(d=1\)的等差数列。

\(\sum S_i=nS_1+\frac{n(n-1)}{2}=\frac{2n(2n+1)}{2}\)

化简得\(S_1=\frac{3(n+1)}{2}\),所以\(n\)为偶数时一定无解。

尝试寻找规律,对\(n=5\)的情况打表可知:

\(S_i\) 9 10 11 12 13
\(a_i\) 3 2 1 5 4
\(b_i\) 6 8 10 7 9

\(i=\frac{(n-1)}{2}\)为分界线,左侧\(a_i=i+1\)时,\(b_i=2n-2i\);右侧\(a_i=\)左侧\(a_i+\frac{(n-1)}{2}\),右侧\(b_i=\)左侧\(b_i+1\)


#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define cerr(x) std::cerr << (#x) << " is " << (x) << '\n'
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);
#define PII pair<int, int>
#define pdd pair<double,double>
#define PLL pair<LL,LL>
#define rep(i, j, k) for(int i=j;i<=k;i++)

#define int long long


const double CLOCKS_PER_SECOND = ((clock_t) 1000);
const double CLOCKS_PER_MILLISECOND = ((clock_t) 1);
const int N = 2e5 + 10, M = 1e8, mod = 1e9 + 7, inf = 0x3f3f3f3f;
const double eps = 1e-6;
//const int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
const int dx[] = {-1, 0, 0, 1}, dy[] = {0, 1, -1, 0};
//#define x first
//#define y second
int fac[N];

int qpow(int a, int b) {
    int ans = 1LL, base = a;
    while (b) {
        if (b & 1) ans = (ans * base) % mod;
        base = (base * base) % mod;
        b >>= 1;
    }
    return ans;
}

int C(int n, int k) {
    if (k > n) return 0;
    return (fac[n] * qpow(fac[k], mod - 2) % mod) * qpow(fac[n - k], mod - 2) % mod;
}

int Lucas(int n, int k) {
    if (!k) return 1LL;
    return C(n % mod, k % mod) * Lucas(n / mod, k / mod) % mod;
}

int T;

int n;

void solve() {
    cin >> n;
    if (!(n & 1)) {
        cout << "NO" << endl;
        return;
    }
    cout << "YES" << endl;
    vector<PII > t;
    t.push_back({1, 2 * n});
    rep(i, 1, (n - 1) / 2) {
        t.push_back({1 + i, 2 * n - 2 * i});
        t.push_back({1 + i + (n - 1) / 2, 2 * n - 2 * i + 1});
    }//{2,8},{3,6}||{2+2,9},{2+3,7}
    
    for (auto i: t) cout << i.first << " " << i.second << endl;
}


signed main() {
    IOS;
    cin >> T;
    while (T--)
        solve();
}
posted @ 2023-02-10 11:14  SxtoxA  阅读(30)  评论(0)    收藏  举报
12 13