0%

思路讲解

这种位运算的题我显然就没有那么会做。

但是不会做手也不能闲着(如果一定要做这道题目的话),可以写个对拍程序呀,或者什么的,打表看看规律。

结果最后还是要按位分析,不过也不用怕,加法的进位其实无所谓的,因为加法与位运算在这道题里面是隔离的。

AC代码

https://codeforces.com/problemset/submission/2057/309432960

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Problem: C. Trip to the Olympiad
// Contest: Codeforces - Hello 2025
// URL: https://codeforces.com/problemset/problem/2057/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(1e6)+10,INF=static_cast<ll>(5e18)+3;

ll L,R,T,A[MAXN],pow2[45],sumPow2[45];

inline void solve(){
cin>>L>>R;
// ll target=0;
// for(int i=L;i<=R;++i){
// for(int j=L;j<=R;++j){
// for(int k=L;k<=R;++k){
// ll ltar=(i^j)+(i^k)+(k^j);
// if(ltar>=target){
// target=ltar;
// ans[0]=i;
// ans[1]=j;
// ans[2]=k;
// }
// }
// }
// }
ll opR=R;
vector<ll> bitR;
while(opR){
bitR.push_back(opR%2);
opR/=2;
}
ll a=R,b=0,c=0;
// 每一位上,110或者100或者101布局是最合理的
for(int i=bitR.size()-1;i>=0;--i){
if(bitR[i]==1){
if(b+pow2[i]<=R && c+sumPow2[i-1]>=L){ // 可不可以110布局
b+=pow2[i];
}else if(b+sumPow2[i-1]>=L && c+pow2[i]<=R){ // 可不可以101布局
c+=pow2[i];
}else if(b+sumPow2[i-1]>=L && c+sumPow2[i-1]>=L){ // 可不可以100布局
continue;
}else if(b+sumPow2[i-1]<L && c+sumPow2[i-1]<L){ // 最后再不行就111布局吧
b+=pow2[i];
c+=pow2[i];
}
}else{
if(b+pow2[i]<=R && c+pow2[i]<=R){ // 011
b+=pow2[i];
c+=pow2[i];
}else if(b+pow2[i]<=R && c+sumPow2[i-1]>=L){ // 010
b+=pow2[i];
}else if(b+sumPow2[i-1]>=L && c+pow2[i]<=R){ // 001
c+=pow2[i];
}else if(c+pow2[i]>R && b+pow2[i]>R){ // 000
continue;
}
}
}
if(a==b) b-=1;
if(b==c) c-=1;
cout<<a<<" "<<b<<" "<<c<<"\n"; // 题目竟然还要求a,b,c不同,离谱
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
pow2[0]=1;
for(int i=1;i<=40;++i){
pow2[i]=2*pow2[i-1];
}
sumPow2[0]=pow2[0];
for(int i=1;i<=40;++i){
sumPow2[i]=sumPow2[i-1]+pow2[i];
}
while(T--){
solve();
}
return 0;
}
/*

*/

心路历程(WA,TLE,MLE……)

题目还要求a,b,c需要不同,我直接相同就-1,虽然我感觉似乎没那么妥当,但AC了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: 1850G-The-Morning-Star
categories:
- codeforces题解
tags:
- null
date: 2025-03-05 11:48:05
---
我们都知道,想要找到同x或者同y的点的数量易如反掌。

那么,同对角线的那?

其实也很简单,找x+y || x-y的值是相同的点的数量就行
```cpp
// Problem: G. The Morning Star
// Contest: Codeforces - Codeforces Round 886 (Div. 4)
// URL: https://codeforces.com/problemset/problem/1850/G
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int) a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef array<ll,3> arr;
typedef double DB;
typedef pair<DB,DB> pdd;
constexpr ll MAXN=static_cast<ll>(2e5)+10,INF=static_cast<ll>(1e18)+3;

ll N,M,T;
pll xy[MAXN];
bool cmpy(const pll &a,const pll &b){
if(a.se!=b.se) return a.se<b.se;
return a.fi<b.fi;
}
bool cmpxPy(const pll &a,const pll &b){
if(a.fi+a.se!=b.fi+b.se) return a.fi+a.se<b.fi+b.se;
return a.fi<b.fi;
}
bool cmpxMy(const pll &a,const pll &b){
if(a.fi-a.se!=b.fi-b.se) return a.fi-a.se<b.fi-b.se;
return a.fi<b.fi;
}
inline void solve(){
cin>>N;
for(int i=1;i<=N;++i){
cin>>xy[i].fi>>xy[i].se;
}
sort(xy+1,xy+1+N);
ll ans=0;
for(int i=1;i<=N;++i){
pll uppConst={xy[i].fi,INF},lowConst={xy[i].fi,-INF};
ans+=upper_bound(xy+1,xy+1+N,uppConst)-lower_bound(xy+1,xy+1+N,lowConst)-1;
}
// #ifdef LOCAL
// cerr << ans << '\n';
// #endif
sort(xy+1,xy+1+N,cmpy);
for(int i=1;i<=N;++i){
pll uppConst={INF,xy[i].se},lowConst={-INF,xy[i].se};
ans+=upper_bound(xy+1,xy+1+N,uppConst,cmpy)-lower_bound(xy+1,xy+1+N,lowConst,cmpy)-1;
}
// #ifdef LOCAL
// cerr << ans << '\n';
// #endif
// 同y和同x的匹配完了,同对角线的怎么统计?
// 哈哈,其实和上面的一样的思路,同一对角线,x-y||x+y的值是一样的
sort(xy+1,xy+1+N,cmpxPy);
for(int i=1;i<=N;++i){
ll aPb=xy[i].fi+xy[i].se;
pll uppConst={INF,aPb-INF},lowConst={-INF,aPb+INF};
ans+=upper_bound(xy+1,xy+1+N,uppConst,cmpxPy)-lower_bound(xy+1,xy+1+N,lowConst,cmpxPy)-1;
}
// #ifdef LOCAL
// cerr << ans << '\n';
// #endif
sort(xy+1,xy+1+N,cmpxMy);
for(int i=1;i<=N;++i){
ll aMb=xy[i].fi-xy[i].se;
pll uppConst={INF,-aMb+INF},lowConst={-INF,-aMb-INF};
ans+=upper_bound(xy+1,xy+1+N,uppConst,cmpxMy)-lower_bound(xy+1,xy+1+N,lowConst,cmpxMy)-1;
}
cout<<ans<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
AC
https://codeforces.com/problemset/submission/1850/309011870
*/

思路讲解

CF-1004-B. Two Large Bags

和这题也没那么像,但是确实这种有操作,而且操作没有次数限制的,一定要找出一种操作模式,虽然说有可能浪费,但是绝对不会恶化现有情况,这是我对这类题目的自己的一点感悟

那么,这道题目,我们发现,如果有一个逆序对(比如说下面这个5,4)

1
2
8
4 5 4 5 4 5 4 5

我们肯定就要处理这个,我们要让5变小

但是这个还要判断逆序对呀,能不能再进一步,对所有数都进行一次这个操作?

我们可以证明,这个操作一定是优的,因为他让前面的数减小了

于是我们顺着操作一遍,再判断是否是有序的就好了

AC代码

https://codeforces.com/contest/2060/submission/305951401

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Problem: D. Subtract Min Sort
// Contest: Codeforces - Codeforces Round 998 (Div. 3)
// URL: https://codeforces.com/contest/2060/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(), x.end()
#define CLR(i, a) memset(i, a, sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int)a.size())

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef array<ll, 3> arr;
typedef double DB;
typedef pair<DB, DB> pdd;
constexpr ll MAXN = static_cast<ll>(2e5) + 10, INF = static_cast<ll>(5e18) + 3;

ll N, T, A[MAXN], backup[MAXN];

inline void solve() {
cin >> N;
FOR(i, 1, N) { cin >> A[i]; }
if (A[1] > A[2]) {
cout << "NO\n";
return;
}
// 我们可以证明,这个操作一定是优的,因为他让前面的数减小了
FOR(i, 1, N - 1) {
ll t1 = A[i] - min(A[i], A[i + 1]), t2 = A[i + 1] - min(A[i], A[i + 1]);
A[i] = t1;
A[i + 1] = t2;
}
FOR(i, 1, N) { backup[i] = A[i]; }
sort(A + 1, A + 1 + N);
FOR(i, 1, N) {
if (backup[i] != A[i]) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> T;
while (T--) {
solve();
}
return 0;
}
/*
AC
https://codeforces.com/contest/2060/submission/305951401
*/

心路历程(WA,TLE,MLE……)

思路讲解

没有在X中的一点,任何一个点都到达不了,因为在有向图中你不发出边是到达不了任何点的

如果所有点都在X中,那么就选择一个q1(X[i]==1 时的i),一个q2(X[i]==N 时的i),他们的曼哈顿距离≥N-1

但是在这个只有N条边的有向图上,你觉得q1到q2以及q2到q1的有向图路径都大于N-1嘛?显然不可能吧,没那么多边呀,借此就可以判断是有向图还是坐标了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
else {  // 这种情况难搞一点
// 因为这种情况理论上来说甚至有可能每个点之间都相互联通在有向图上
// 但我们可以利用曼哈顿距离的特性来解决
ll q1=0, q2=0;
FOR(i, 1, N) {
if (X[i] == 1)
q1 = i;
else if (X[i] == N)
q2 = i;
}
cout << "? " << q1 << ' ' << q2 << endl;
ll dis1, dis2;
cin >> dis1;
if (dis1 < N - 1) { // dis1比最小曼哈顿距离都小,肯定也不对
cout << "! A" << endl;
return true;
}
cout << "? " << q2 << ' ' << q1 << endl;
cin >> dis2;
if (dis2 < N - 1) { // dis2比最小曼哈顿距离都小,肯定也不对
cout << "! A" << endl;
return true;
}
cout << "! B" << endl;
}

AC代码

AC

https://codeforces.com/contest/2067/submission/305909277

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Problem: D. Object Identification
// Contest: Codeforces - Codeforces Round 1004 (Div. 2)
// URL: https://codeforces.com/contest/2067/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(), x.end()
#define CLR(i, a) memset(i, a, sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int)a.size())
#define LOCAL

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef array<ll, 3> arr;
typedef double DB;
typedef pair<DB, DB> pdd;
constexpr ll MAXN = static_cast<ll>(2e5) + 10, INF = static_cast<ll>(5e18) + 3;

ll N, T, X[MAXN];
bool vis[MAXN];

inline bool solve() {
cin >> N;
if (N == -1) {
return false;
}
FOR(i, 1, N) { vis[i] = false; }
FOR(i, 1, N) {
cin >> X[i];
vis[X[i]] = true;
}
vector<ll> disN; // 没有在X中出现的点
ll app; // 随便取一个出现过的点
FOR(i, 1, N) {
if (!vis[i])
disN.pb(i);
else
app = i;
}
if (SZ(disN) >= 1) {
// 没有在X中的一点,任何一个点都到达不了,因为在有向图中你不发出边是到达不了任何点的
cout << "? " << disN[0] << ' ' << app << endl;
ll dis;
cin >> dis;
if (dis == 0)
cout << "! A" << endl;
else
cout << "! B" << endl;
} else { // 这种情况难搞一点
// 因为这种情况理论上来说甚至有可能每个点之间都相互联通在有向图上
// 但我们可以利用曼哈顿距离的特性来解决
ll q1=0, q2=0;
FOR(i, 1, N) {
if (X[i] == 1)
q1 = i;
else if (X[i] == N)
q2 = i;
}
cout << "? " << q1 << ' ' << q2 << endl;
ll dis1, dis2;
cin >> dis1;
if (dis1 < N - 1) { // dis1比最小曼哈顿距离都小,肯定也不对
cout << "! A" << endl;
return true;
}
cout << "? " << q2 << ' ' << q1 << endl;
cin >> dis2;
if (dis2 < N - 1) { // dis2比最小曼哈顿距离都小,肯定也不对
cout << "! A" << endl;
return true;
}
cout << "! B" << endl;
}
return true;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> T;
while (T--) {
bool flag = solve();
if (!flag) return 0;
}
return 0;
}
/*
AC
https://codeforces.com/contest/2067/submission/305909277
*/

心路历程(WA,TLE,MLE……)

思路讲解

https://codeforces.com/blog/entry/139415?locale=en

image

就只看最后一位数(因为最后一位数不会有进位什么的问题),就可以得到最多不会超过9次的结论

然后就直接暴力就好了,不知道题解后面在写什么

AC代码

https://codeforces.com/contest/2067/submission/305782258

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Problem: C. Devyatkino
// Contest: Codeforces - Codeforces Round 1004 (Div. 2)
// URL: https://codeforces.com/contest/2067/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// by znzryb
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (int i = (a); i >= (b); --i)
#define all(x) x.begin(), x.end()
#define CLR(i, a) memset(i, a, sizeof(i))
#define fi first
#define se second
#define pb push_back
#define SZ(a) ((int)a.size())
#define LOCAL

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef array<ll, 3> arr;
typedef double DB;
typedef pair<DB, DB> pdd;
constexpr ll MAXN = static_cast<ll>(1e6) + 10, INF = static_cast<ll>(5e18) + 3;

ll N, T;
ll pow10[20];

inline void solve() {
ll x;
cin >> x;
ll ans = 9;
string s = to_string(x);
N = SZ(s);
FOR(i, 0, N - 1) {
if (s[i] == '7') {
cout << 0 << '\n';
return;
}
}
FOR(i, 1, N + 1) {
ll add = pow10[i] - 1;
ll t = x;
FOR(j, 1, ans) {
t += add;
string ts = to_string(t);
bool isBreak = false;
FOR(k, 0, SZ(ts) - 1) {
if (ts[k] == '7') {
ans = j;
// #ifdef LOCAL
// FOR(lk, 0, SZ(ts) - 1) { cerr << ts[lk]; }
// cerr << '\n';
// #endif
isBreak = true;
break;
}
}
if (isBreak) break;
}
}
cout << ans << '\n';
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> T;
pow10[0] = 1;
FOR(i, 1, 18) { pow10[i] = pow10[i - 1] * 10; }
while (T--) {
solve();
// #ifdef LOCAL
// cerr << '\n';
// #endif
}
return 0;
}
/*
AC
https://codeforces.com/contest/2067/submission/305782258
*/

心路历程(WA,TLE,MLE……)