原题:
E. Game of the Year
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Monocarp and Polycarp are playing a computer game. This game features n bosses for the playing to kill, numbered from 1 to n.
They will fight each boss the following way:
Monocarp makes k
attempts to kill the boss;
Polycarp makes k
attempts to kill the boss;
Monocarp makes k
attempts to kill the boss;
Polycarp makes k
attempts to kill the boss;
…
Monocarp kills the i-th boss on his ai-th attempt. Polycarp kills the i-th boss on his bi-th attempt. After one of them kills the i-th boss, they move on to the (i+1)-st boss. The attempt counters reset for both of them. Once one of them kills the n-th boss, the game ends.
Find all values of kfrom 1 to n such that Monocarp kills all bosses.
Input
The first line contains a single integer t (1≤t≤ 1 0 4 10^4 104) — the number of testcases.
The first line of each testcase contains a single integer n (1≤n≤2⋅ 1 0 5 10^5 105) — the number of bosses.
The second line contains n integers a1,a2,…,an (1≤ a i a_i ai≤n) — the index of attempt Monocarp kills each boss on.
The third line contains n integers b1,b2,…,bn (1≤b_i≤n) — the index of attempt Polycarp kills each boss on.
The sum of n over all testcases doesn’t exceed 2⋅ 1 0 5 10^5 105.
Output
For each testcase, print two lines. The first line should contain a single integer cnt — the number of values of k from 1 to n such that Monocarp kills all bosses. The second line should contain cnt distinct integers — the values of k themselves.
Example
Input
3
3
1 1 1
2 3 1
1
1
1
4
1 4 3 2
3 3 4 1
Output
3
1 2 3
1
1
2
2 4
Note
Consider the last testcase of the example.
Let k=1 . First, Monocarp makes one attempt to kill the first boss. It’s successful, since a1=1. Then, Monocarp makes one attempt to kill the second boss. It’s unsuccessful, since a2>1. So, Polycarp makes an attempt then. It’s also unsuccessful, since b2>1. Then, Monocarp makes another attempt. It’s still unsuccessful, since a2>2. This goes on until Polycarp finally kills the boss on his third attempt. Monocarp didn’t kill this boss, thus, k=1 isn’t the answer.
Let k=2. Monocarp still kills the first boss on his first attempt. Then, he makes two unsuccessful attempts for the second boss. Then, Polycarp makes two unsuccessful attempts. Then, Monocarp makes two more attempts and kills the boss on his fourth attempt. The third boss is similar. First, two unsuccessful attempts by Monocarp. Then, two unsuccessful attempts by Polycarp. Then, Monocarp has two more attempts, but even his first one is successful, since a3=3. The fourth boss is also killed by Monocarp. Thus, k=2 is the answer.
中文:
两列数, a i a_i ai和 b i b_i bi, a i a_i ai和 b i b_i bi轮流减去k, a i a_i ai先手,如果有某个k值使得都是 a i a_i ai先减为0,则k使其中一个答案,现在问你所有可行的k。
代码:
#include
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
ll a[maxn], b[maxn];
ll mark[maxn];
vector<ll> ans;
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
ans.clear();
memset(mark, 0, sizeof(mark));
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
if(b[i] < a[i]) {
mark[b[i]]++;
mark[a[i]]--;
}
}
for (int i = 0; i < n; i++) {
mark[i + 1] += mark[i];
}
for (int k = 1; k <= n; k++) {
int mk = k;
int flag = 1;
while(mk <= n) {
if (mark[mk] != 0) {
flag = 0;
break;
}
mk += k;
}
if (flag) {
ans.push_back(k);
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
if (i != ans.size() - 1) {
cout << ans[i] << " ";
} else {
cout << ans[i] << endl;
}
}
}
return 0;
}
/*
1 9 9 6 1 1 1 1 1 1 1 7 5 2 2 2 2 2 2 2
*/
解答:
在纸上算算可以发现这样的规律,如果 a i ≤ b i a_i \leq b_i ai≤bi,对于任意的k值肯定成立,所以关键考虑 a i > b i a_i > b_i ai>bi的情况。
如果 a i > b i a_i > b_i ai>bi,在纸上算一下,发现满足情况 ⌈ a i k ⌉ > ⌈ b i k ⌉ \lceil\frac{a_i}{k}\rceil > \lceil\frac{b_i}{k}\rceil ⌈kai⌉>⌈kbi⌉时的k值取出即可,即满足 [ b i , a i ) [b_i, a_i) [bi,ai),有一个k的倍数。
弄个前缀和mark,记录所有 [ b i , a i ) [b_i, a_i) [bi,ai)的区间。然后暴力枚举k的倍数mk,判断mk是否在区间即可