当前位置:网站首页>Codeforces Round #804 (Div. 2) - A, B, C
Codeforces Round #804 (Div. 2) - A, B, C
2022-07-05 20:08:00 【小酒窝.】
https://codeforces.com/contest/1699
C. The Third Problem
题意
给定一个 0 0 0 到 n − 1 n-1 n−1 的全排列,问一共有多少 0 0 0 到 n − 1 n-1 n−1 的全排列和该排列相似?
答案对 1e9+7 取模。
相似定义:如果两个全排列满足下面条件,就说两个这两个全排列相似。
- 对于任意区间 [ l , r ] ( 1 ≤ l ≤ r ≤ n ) [l, r] (1 \le l \le r \le n) [l,r](1≤l≤r≤n) 都满足: MEX ( [ a l , a l + 1 , … , a r ] ) = MEX ( [ b l , b l + 1 , … , b r ] ) \operatorname{MEX}([a_l,a_{l+1},\ldots,a_r])=\operatorname{MEX}([b_l,b_{l+1},\ldots,b_r]) MEX([al,al+1,…,ar])=MEX([bl,bl+1,…,br])
对于一堆数 c 1 , c 2 , … , c k c_1,c_2,\ldots,c_k c1,c2,…,ck 的 MEX \operatorname{MEX} MEX 是指,在集合 c c c 中第一个未出现的非负整数 x x x。
思路
从 0 到 n-1,一个数一个数的判断,看其能放在哪些位置。所有可能的位置数相乘就是答案。
标记数 x 在原排列中所在位置为 p[x]。
- 首先容易确定,0 和 1 在相似排列中的位置不变,都为
p[0]。设l为较左端位置,r为较右端位置。 - 对于 2:
1. 如果p[2]位于[l, r]中,那么 2 可以放在[l, r]中的所有空闲位置;
(2 原来在[l, r]中,那么 MEX [ l , r ] \operatorname{MEX}[l, r] MEX[l,r] 就达到 3 了,所以在相似排列中 2 只能放在[l, r]中)
2. 否则,2 只能放在原来位置p[2];然后p[2]将l或者r更新;
(2 原来在[l, r]之外,那么 MEX [ l , r ] \operatorname{MEX}[l, r] MEX[l,r] 只能达到 2,2不能在[l, r]中;而其位置如果改变的话,会有区间的 MEX 值改变,所以这种情况下 2 的位置不能改变) - 对于 3:
1. 如果p[3]位于[l, r]中,那么 3 可以放在[l, r]中的所有空闲位置;
2. 否则,3 只能放在原来位置p[3];然后p[3]将l或者r更新; - …
Code
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N], p[N];
signed main(){
Ios;
cin >> T;
while(T--)
{
cin >> n;
for(int i=1;i<=n;i++) cin >> a[i], p[a[i]] = i;
int ans = 1;
int l = min(p[0], p[1]), r = max(p[0], p[1]);
for(int i=2;i<n;i++)
{
if(p[i] > l && p[i] < r)
{
ans = ans * (r-l+1 - i) % mod;
}
else{
if(p[i] < l) l = p[i];
else r = p[i];
}
}
cout << ans << endl;
}
return 0;
}
经验
当没有思路的时候应该沉下心来从最初始的情况一点一点往后推,也许推着推着就有思路了。
而不是看着样例胡乱想。。
B. Almost Ternary Matrix
题意
构造一个 n*m 的 01 矩阵,使得:
- 对于每个位置,其直接相邻的位置中,恰有两个位置和该位置元素不同。
2 ≤ n , m ≤ 50 2 \le n,m \le 50 2≤n,m≤50 且都为偶数。
思路
这样构造:
6 8
1 0 0 1 1 0 0 1
0 1 1 0 0 1 1 0
0 1 1 0 0 1 1 0
1 0 0 1 1 0 0 1
1 0 0 1 1 0 0 1
0 1 1 0 0 1 1 0
Code
#include<bits/stdc++.h>
using namespace std;
const int N = 210, mod = 1e9+7;
int T, n, m;
int a[N] = {
1, 0, 0, 1}, b[N] = {
0, 1, 1, 0};
int ans[N][N];
signed main(){
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i=1;i<=n;i++)
{
for(int j=0;j<m;j++)
{
if(i%4 == 1 || i%4 == 0) ans[i][j] = a[j%4];
else ans[i][j] = b[j%4];
}
}
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++){
printf("%d ", ans[i][j]);
}
printf("\n");
}
}
return 0;
}
A. The Third Three Number Problem
题意
给定一个数 n n n,找到三个数 a , b , c a, b, c a,b,c 满足:
- ( a ⊕ b ) + ( b ⊕ c ) + ( a ⊕ c ) = n (a\oplus b)+(b\oplus c)+(a\oplus c)=n (a⊕b)+(b⊕c)+(a⊕c)=n
找不到输出 − 1 -1 −1。
1 ≤ n ≤ 1 0 9 , 0 ≤ a , b , c ≤ 1 0 9 1 \le n \le 10^9,\ 0 \le a, b, c \le 10^9 1≤n≤109, 0≤a,b,c≤109
思路
由 a + b = a ⊕ b + 2 ⋅ ( a a + b = a \oplus b + 2 \cdot (a a+b=a⊕b+2⋅(a & b ) b) b) 可知, a ⊕ b a \oplus b a⊕b 和 a + b a + b a+b 具有相同的奇偶性。
那么 n = ( a ⊕ b ) + ( b ⊕ c ) + ( a ⊕ c ) n = (a\oplus b)+(b\oplus c)+(a\oplus c) n=(a⊕b)+(b⊕c)+(a⊕c) 就和 ( a + b ) + ( b + c ) + ( a + c ) = 2 ⋅ ( a + b + c ) (a+b) + (b+c) + (a+c) =2 \cdot (a+b+c) (a+b)+(b+c)+(a+c)=2⋅(a+b+c) 具有相同奇偶性,一定为偶数。
- 当 n 为 奇数时输出 -1。
- 否则 令 b = c = 0 b=c=0 b=c=0,那么原式变为: a + a = n a + a = n a+a=n,那么 a = n 2 a = \frac n 2 a=2n。
Code
#include<bits/stdc++.h>
using namespace std;
#define Ios ios::sync_with_stdio(false),cin.tie(0)
const int N = 200010, mod = 1e9+7;
int T, n, m;
int a[N];
signed main(){
Ios;
cin >> T;
while(T--)
{
cin >> n;
if(n % 2 == 0) cout << n/2 << " " << 0 << " " << 0 << endl;
else cout << -1 << endl;
}
return 0;
}
经验
着重考虑特殊情况。
边栏推荐
- JVMRandom不可设置种子|问题追溯|源码追溯
- Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
- How to select the Block Editor? Impression notes verse, notation, flowus
- Go language | 01 wsl+vscode environment construction pit avoidance Guide
- js方法传Long类型id值时会出现精确损失
- sun. misc. Base64encoder error reporting solution [easy to understand]
- Leetcode brush question: binary tree 13 (the same tree)
- [C language] three implementations of quick sorting and optimization details
- Add data to excel small and medium-sized cases through poi
- DP:树DP
猜你喜欢

Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
Android interview classic, 2022 Android interview written examination summary

Leetcode brush questions: binary tree 18 (largest binary tree)
![[hard core dry goods] which company is better in data analysis? Choose pandas or SQL](/img/70/a79c4a1724c11e208814de2d9cf553.png)
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL

Unity编辑器扩展 UI控件篇

B站UP搭建世界首个纯红石神经网络、基于深度学习动作识别的色情检测、陈天奇《机器学编译MLC》课程进展、AI前沿论文 | ShowMeAI资讯日报 #07.05

Wechat applet regular expression extraction link

Leetcode skimming: binary tree 12 (all paths of binary tree)

Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder

Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
随机推荐
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
Go language learning tutorial (16)
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
95后阿里P7晒出工资单:狠补了这个,真香...
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
Let's talk about threadlocalinsecurerandom
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
[C language] three implementations of quick sorting and optimization details
leetcode刷题:二叉树15(找树左下角的值)
Is it safe for Anxin securities to open an account online?
建立自己的网站(16)
Parler de threadlocal insecurerandom
股票开户哪里好?网上客户经理开户安全吗
selenium 元素信息
期货如何网上开户?安不安全?
Oracle-表空间管理
Scala基础【HelloWorld代码解析,变量和标识符】
Process file and directory names