Codeforces Round #851 (Div. 2) A~C
题之后补吧
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
按位拆分,如果的第位为偶数,令,否则交替令相差。
#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
是个满足的等差数列。
故
化简得,所以为偶数时一定无解。
尝试寻找规律,对的情况打表可知:
9 | 10 | 11 | 12 | 13 | |
---|---|---|---|---|---|
3 | 2 | 1 | 5 | 4 | |
6 | 8 | 10 | 7 | 9 |
以为分界线,左侧时,;右侧左侧,右侧左侧。
#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();
}
分类:
题解
, CodeForces
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!