Codeforces Round #807 (Div. 2)
Difference of GCDs
题意
从l到r中找到n个数,使得gcd(i,a[i]) 唯一或不存在这样的序列
思路
gcd(i,a[i]) 唯一很容易想到a[i]是i 的倍数即可,开始想的从l到r中挨个找符合条件即可。但是这样可能会漏掉,因为gcd唯一而l到r中的数可以多次使用。所以我们从1~n遍历每个 i 如果 l%i==0 则l为答案,否则答案为l + (i - l % i)。
#define _CRT_SECURE_NO_WARNINGS 1;
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
using namespace std;
#define off ios::sync_with_stdio(false);
#define INF 0x3f3f3f3f
#define forn(i,n) for(int i = 0; i < int(n); i++)
#define scan1(n) scanf("%d",&n)
#define scan2(n,m) scanf("%d %d",&n,&m)
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c)
const int N = 1e5 + 10;
const int M = 1e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int, int> PII;
typedef long long ll;
int a[N];
int main()
{
//off;
int t;
scan1(t);
while (t--) {
int n, l, r;
scan3(n, l, r);
int f = 0;
for (int i = 1; i <= n; i++) {
if (l % i == 0) a[i] = l;
else a[i] = l + (i - l % i);
if (a[i] > r) {
f = 1;
break;
}
}
if (f) cout << "NO" << endl;
else {
cout << "YES" << endl;
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
cout << endl;
}
}
return 0;
}
题意
长度为n的序列和初始值iq,对于每个数,如果要选取
1. 如果a[i] <= iq 无事发生
2. 如果a[i] > iq iq--
不选取无事发生,输出n个数,对于第i个数如果选取答案这一位为1,否则为0。输出选取最多的情况。
#define _CRT_SECURE_NO_WARNINGS 1;
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
using namespace std;
#define off ios::sync_with_stdio(false);
#define INF 0x3f3f3f3f
#define forn(i,n) for(int i = 0; i < int(n); i++)
#define scan1(n) scanf("%d",&n)
#define scan2(n,m) scanf("%d %d",&n,&m)
#define scan3(a,b,c) scanf("%d %d %d",&a,&b,&c)
const int N = 1e5 + 10;
const int M = 1e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int, int> PII;
typedef long long ll;
int a[N];
int ans[N];
int n, iq;
bool check(int mid, int q) {
for (int i = 1; i <= n; i++) ans[i] = 0;
for (int i = 1; i < mid; i++) {
if (a[i] <= q) ans[i] = 1;
}
for (int i = mid; i <= n; i++) {
ans[i] = 1;
if (q == 0) return 0;
if (a[i] > q) q--;
}
if (q < 0) return 0;
return 1;
}
int main()
{
off;
int t;
cin >> t;
while (t--) {
cin >> n >> iq;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int l = 1, r = n;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid, iq)) r = mid - 1;
else l = mid + 1;
}
check(r + 1, iq);
for (int i = 1; i <= n; i++) cout << ans[i];
cout << endl;
}
return 0;
}