当前位置:网站首页>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;
}
经验
着重考虑特殊情况。
边栏推荐
- Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
- 使用 RepositoryProvider简化父子组件的传值
- Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
- leetcode刷题:二叉树11(平衡二叉树)
- 怎么挑选好的外盘平台,安全正规的?
- gst-launch的-v参数
- Using repositoryprovider to simplify the value passing of parent-child components
- 2022年7月4日-2022年7月10日(ue4视频教程mysql)
- 解决php无法将string转换为json的办法
- 【obs】libobs-winrt :CreateDispatcherQueueController
猜你喜欢
[untitled]
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
leetcode刷题:二叉树12(二叉树的所有路径)
Leetcode brush question: binary tree 14 (sum of left leaves)
浅浅的谈一下ThreadLocalInsecureRandom
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
A solution to PHP's inability to convert strings into JSON
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
leetcode刷题:二叉树10(完全二叉树的节点个数)
【数字IC验证快速入门】8、数字IC中的典型电路及其对应的Verilog描述方法
随机推荐
Base du réseau neuronal de convolution d'apprentissage profond (CNN)
A solution to PHP's inability to convert strings into JSON
港股将迎“最牛十元店“,名创优品能借IPO突围?
Fundamentals of deep learning convolutional neural network (CNN)
Build your own website (16)
Ffplay document [easy to understand]
Leetcode brush questions: binary tree 18 (largest binary tree)
Android interview classic, 2022 Android interview written examination summary
Where is the operation of new bonds? Is it safer and more reliable to open an account
MySql的root密码忘记该怎么找回
Securerandom things | true and false random numbers
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
[C language] three implementations of quick sorting and optimization details
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
[untitled]
炒股开户最低佣金,低佣金开户去哪里手机上开户安全吗
leetcode刷题:二叉树11(平衡二叉树)
【c语言】快速排序的三种实现以及优化细节
How to safely and quickly migrate from CentOS to openeuler
Wildcard selector