这里需要注意一个特殊情况,就是 w 为 2 的时候,2 是偶数,但是只能分成 1 和 1 ,做一个特判即可。
ac−std
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long LL;
const int N = 2e6 + 10;
int w;
inline void solve() {
cin >> w;
if (w == 2) {
printf("No\n");
return;
}
if (w & 1) printf("No\n");
else printf("Yes\n");
}
int main() {
int _ = 1; cin >> _;
while (_--) solve();
return 0;
}
#include <bits/stdc++.h>
using namespace std;
long long candy[1000005];
bool cmp(long long a, long long b) {
return a > b;
}
int main() {
int n, m; cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> candy[i];
}
sort(candy, candy + n, cmp);
long long ans1 = 0, ans2 = 0;
for (int i = 0; i < m; i++) {
if (i % 2 == 0) {
ans1 += candy[i];
} else {
ans2 += candy[i];
}
}
cout << ans1 << ' ' << ans2;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0); // 关闭同步流
cin.tie(0);
cout.tie(0);
int a, b; // 输入的起始页码和结束页码
cin >> a >> b;
// 书的装订方式:第 (2k-1) 页和第 (2k) 页在同一张纸上。
// 因此,页码 p 所在的纸张编号可以计算为 (p+1)/2 (使用整数除法)。
// 例如:
// 页1: (1+1)/2 = 1号纸
// 页2: (2+1)/2 = 1号纸
// 页3: (3+1)/2 = 2号纸
// 计算起始页码 a 所在的纸张编号 sa
int sa = (a + 1) / 2;
// 计算结束页码 b 所在的纸张编号 ea
int ea = (b + 1) / 2;
// 需要撕掉的纸张包括从第 sa 张到第 ea 张 (含首尾)。
// 总张数为 ea - sa + 1。
int res = ea - sa + 1;
cout << res << endl;
return 0;
}
n 和 k 最大都是 109,因此指数 nk 最大为 1018,必须使用 long long 保存。
不需要利用费马小定理缩小指数。快速幂的循环次数只有
O(log(nk)),
可以直接处理 1018 级别的指数。
参考实现
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll md = 1000000007;
ll qp(ll a, ll b) {
ll r = 1;
while(b) {
if(b & 1) r = r * a % md;
a = a * a % md;
b >>= 1;
}
return r;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, k;
cin >> n >> k;
cout << qp(2, n * k) << '\n';
return 0;
}
/*
题意:求所有线段子集的并集连通块数量之和。
思路:每个连通块由左端点最小的线段唯一代表。扫描端点,当遇到第 k 个左端点时,
已有 c 条线段完全结束。让当前线段成为新连通块起点的方案数为 2^(c+n-k)。
*/
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int mod=1000000007;
int l[200005];
int r[200005];
ll pw[100005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int x,y;
cin >> x >> y;
l[x] = 1;
r[y] = 1;
}
pw[0] = 1;
for (int i = 1; i <= n; i++)
pw[i] = pw[i - 1] * 2 % mod;
ll ans = 0;
int c = 0;
int k = 0;
for (int i = 1; i <= 2 * n; i++) {
if (l[i]) {
k++;
ans = (ans + pw[c + n - k]) % mod;
}
if (r[i]) c++;
}
cout << ans;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128_t;
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
ll mul(ll a, ll b, ll m) {
return (i128)a * b % m;
}
ll qp(ll a, ll b, ll m) {
ll r = 1;
while (b) {
if (b & 1) r = mul(r, a, m);
a = mul(a, a, m);
b >>= 1;
}
return r;
}
bool mr(ll n) {
if (n < 2) return 0;
for (ll p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
if (n % p == 0) return n == p;
}
ll d = n - 1;
int s = 0;
while ((d & 1) == 0) {
d >>= 1;
s++;
}
for (ll a : {2LL, 325LL, 9375LL, 28178LL,
450775LL, 9780504LL, 1795265022LL}) {
if (a % n == 0) continue;
ll x = qp(a % n, d, n);
if (x == 1 || x == n - 1) continue;
bool ok = 0;
for (int i = 1; i < s; i++) {
x = mul(x, x, n);
if (x == n - 1) {
ok = 1;
bre