Codeforces Round 873 (Div. 2)
Codeforces Round 873 (Div. 2)
链接
A题
打印2-n并且计算总和,然后找到严格大于sum的n的倍数记为x,然后用这个x减去sum得到a.
然后先打印a然后再打印2-n
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no cout<<"NO"<<'\n'
using namespace std;
const int N = 100008;
void solve() {
int n;
scanf("%lld", &n);
vector <int> ans;
int sum = 0;
for (int i = 2; i <= n; i++) {
sum += i;
}
int x = ((sum / n) + 1) * n;//找到严格大于sum的n的倍数x
// cout<<sum<<'\n';
cout << x - sum << ' ';//打印x-sum
for (int i = 2; i <= n; i++) {//打印2-n
cout << i << ' ';
}
cout << '\n';
}
signed main () {
// std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
int t;
cin >> t;
while (t) {
solve();
t--;
}
return 0;
}
B题
计算a[i]和应该正确所在的位置x计算abs(a[i]-x),一直计算这个的gcd最后打印结果
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no cout<<"NO"<<'\n'
using namespace std;
const int N = 100008;
int a[N];
void solve() {
int n;
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
int gcd;//定义gcd
int num = 0;
for (int i = 1; i <= n; i++) {
int cnt = abs(i - a[i]);
if (cnt != 0) {
if (num == 0) {
gcd = cnt;//如果是第一次进入gcd=cnt
num++;
} else {//否则
//一直计算gcd
gcd = __gcd(cnt, gcd);
}
}
}
cout << gcd << '\n';//打印最后的gcd
}
signed main () {
// std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
int t;
cin >> t;
while (t) {
solve();
t--;
}
return 0;
}
C题
先将a,b数组排序,然后使用a数组来二分查找b数组,并且用一个ans来记录答案,还需要一个num来记录前面有几个数填入了对应的位置.最后打印ans
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstring>
#include <unordered_set>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <sstream>
#include <queue>
#define int long long
#define yes cout<<"YES"<<'\n'
#define no cout<<"NO"<<'\n'
using namespace std;
const int N = 200008;
int a[N];
int b[N];
void solve() {
int n;
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
}
sort(a + 1, a + n + 1);
sort(b + 1, b + n + 1);//排序
// for(int i=1;i<=n;i++){
// printf("%lld ",a[i]);
// }
// printf("\n");
// for(int i=1;i<=n;i++){
// printf("%lld ",b[i]);
// }
// printf("\n");
int ans = 1;
int num = 0;
for (int i = 1; i <= n; i++) {
int l = 0, r = n + 1;
while (l + 1 != r) {
int mid = (l + r) / 2;
if (b[mid] < a[i]) {//查找严格小于a[i]的那个数所在的下标
l = mid;
} else {
r = mid;
}
}
// if(l!=0){
// cout<<l<<'\n';
ans = ans * (l - num);//每次累乘更新答案
ans %= 1000000007;//记得mod1e9+7
// cout<<l-num<<'\n';
num++;
// }
}
ans %= 1000000007;
cout << ans << '\n';//打印结果
}
signed main () {
// std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
int t;
cin >> t;
while (t) {
solve();
t--;
}
return 0;
}