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位为偶数,令ai=bi=ni2,否则交替令aibi相差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

Si是个满足d=1的等差数列。

Si=nS1+n(n1)2=2n(2n+1)2

化简得S1=3(n+1)2,所以n为偶数时一定无解。

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

Si 9 10 11 12 13
ai 3 2 1 5 4
bi 6 8 10 7 9

i=(n1)2为分界线,左侧ai=i+1时,bi=2n2i;右侧ai=左侧ai+(n1)2,右侧bi=左侧bi+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 @   SxtoxA  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
12 13
点击右上角即可分享
微信分享提示