day7: 今天OI打了360分,本来可以AK的,但是最后一题dp前缀和优化爆int了,还是那句话,不开LL见祖宗。整体不难,下次还填非常简单。
A: 水题,二进制枚举,把每个数和最后一个数取并,看二进制下1的数量>= k即可,总复杂度O(n)。赛时100分。
B: 突然发现几个题都和二进制有关,有两个整数 x,y,现给出x&y, x∣y, x−y,推出 x,y 分别是多少。
容易发现,x&y^x|y = x + y - 2(x | y),然后变成加减消元了,简单。
C: 有一个包含 [1,v] 中所有整数的集合,他从这个集合中取出了两个没有公共元素的子集,一个包含 cnt1 个不能被 x 整除的数字,另一个包含 cnt2 个不能被 y 整除的数字。求 v 的最小值。
这是一个隐晦的二分题目。可以发现cnt1,cnt2越大,则v越大。考虑二分答案,check函数查询[1,v]时是否满足条件,已知一个数是 x 的倍数且不为 y 的倍数时,一定分给 y ,同理求出在xy区间内一定分给两个cnt的个数。而在每个完整的xy区间内,会有 x-1 个数给 y ,y - 1个数给 x。而xy(x, y 的最小公倍数) 就不会给出去。如果剩下的数(分给两个数都行) 的数量 >= cnt1 + cnt2证明符合条件,返回是,然后正常二分就行。
D: 给定数字 n,a,b,k,初始时设 x = a,进行 k 次操作,每次选择一个 [1,n] 内的整数 y(y≠x),满足 ∣x−y∣<∣x−b∣,然后令 x=y。问不同的操作方案数,某一次操作选择的整数不同即视为不同方案。
本来没看出来,后面一看就知道要前缀和优化dp才能过,设dp[i][j]:移动 i 次后 x = j 的方案数 (前缀和优化 dp),可以滚动数组优化成一维。注意区间判断,我赛时改了15分钟才对。总时间复杂度O(nk),空间复杂度O(n),可以顺利通过此题。
转移:
dp[a] = 1;
for(int i = 1; i <= k; i++) {
vector<int> s(n+1, 0);
for(int l = 1; l <= n; l++) s[l] = (s[l-1] + dp[l]) % mod;
for(int j = 1; j <= n; j++) {
if(j == b) continue;
else if(j < b) {
int dlt = b - j, st = (dlt - 1) >> 1;
dp[j] = (0ll + s[j + st] + (mod - dp[j])% mod) % mod;
} else if(j > b) {
int dlt = j - b, st = (dlt - 1) >> 1;
dp[j] = (0ll + s[n] + (mod - s[j-st-1]) % mod + (mod - dp[j]) % mod) % mod;
}
}
}本来这个题能过,但是没有加0ll所以赛时只拿了60分,可惜我的AK。
总体上题单不难,希望下次难点。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
ll check(ll n) {
ll res = 0;
while(n > 0) {
if(n & 1) res++;
n >>= 1;
}
return res;
}
void work() {
ll n, m, k;
cin >> n >> m >> k;
vector<ll> a(m+2, 0);
for(int i = 1; i <= m; i++) {
cin >> a[i];
}
ll tar, ans = 0;
cin >> tar;
for(int i = 1; i <= m; i++) {
ll now = tar|a[i];
if(check(now) >= k) ans++;
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
return 0;
}#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
void work() {
ll a, b, c;
cin >> a >> b >> c;
ll x = a, y = a;
ll sum = a^b, del = c;
x += (sum + del) / 2;
y += (sum - del) / 2;
cout << x << ' ' << y << '\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
while(T--) {
work();
}
return 0;
}#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
ll x, y, cnt1, cnt2;
bool check(ll n) {
ll zn = n / (x * y), nres = n % (x * y);
ll xn = zn * (x - 1), yn = zn * (y - 1);
ll xr = nres / y, yr = nres / x;
xn += xr, yn += yr;
ll c1 = cnt1, c2 = cnt2;
if(c1 <= xn && c2 <= yn) return 1;
if(c1 > xn) c1 -= xn;
else c1 = 0;
if(c2 > yn) c2 -= yn;
else c2 = 0;
ll res = n - xr - yr - zn*(x + y - 1);
if(c1 + c2 <= res) return 1;
return 0;
}
void work() {
cin >> cnt1 >> cnt2 >> x >> y;
if(x > y) {
swap(x, y); swap(cnt1, cnt2);
}
if(min(x, y) - 1 >= cnt1 + cnt2) {
cout << cnt1 + cnt2 << '\n';
return;
}
ll l = 1, r = 2e9, ans = 0;
while(l <= r) {
ll mid = (l + r) >> 1;
if(check(mid)) {
ans = mid; r = mid - 1;
} else l = mid + 1;
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
return 0;
}#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
const int mod = 1e9 + 7;
void work() {
int n, a, b, k;
cin >> n >> a >> b >> k;
//dp[i][j]:移动 i 次后 x = j的方案数 (前缀和优化 dp)
vector<int> dp(n+10, 0);
int del = abs(a - b);
// for(int i = max(1, a - del + 1); i <= min(n, a + del - 1); i++) {
// if(i != a) dp[i] = 1;
// }
dp[a] = 1;
for(int i = 1; i <= k; i++) {
vector<int> s(n+1, 0);
for(int l = 1; l <= n; l++) s[l] = (s[l-1] + dp[l]) % mod;
for(int j = 1; j <= n; j++) {
if(j == b) continue;
else if(j < b) {
int dlt = b - j, st = (dlt - 1) >> 1;
dp[j] = (0ll + s[j + st] + (mod - dp[j])% mod) % mod;
} else if(j > b) {
int dlt = j - b, st = (dlt - 1) >> 1;
dp[j] = (0ll + s[n] + (mod - s[j-st-1]) % mod + (mod - dp[j]) % mod) % mod;
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++) {
ans = (ans + dp[i]) % mod;
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
int T = 1;
// cin >> T;
while(T--) {
work();
}
return 0;
}