QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#770265 | #9738. Make It Divisible | mapleKing | WA | 1ms | 3648kb | C++20 | 4.9kb | 2024-11-21 21:18:13 | 2024-11-21 21:18:14 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize(2)
// using namespace std;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
const int inf = 1e9;
template<class Info>
struct SegmentTree {
int n;
std::vector<Info> info;
SegmentTree(int n) : n(n), info(4 << std::__lg(n + 1)) {}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r == l) {
info[p] = v;
return;
}
int m = (l + r) / 2;
if (x <= m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m + 1, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 1, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l > y || r < x) {
return {inf};
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m + 1, r, x, y);
}
Info rangeQuery(int l, int r) {
if (l > r) return {inf};
return rangeQuery(1, 1, n, l, r);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l > y || r < x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r == l) {
return l;
}
int m = (l + r) / 2;
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m + 1, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 1, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l > y || r < x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r == l) {
return l;
}
int m = (l + r) / 2;
int res = findLast(2 * p + 1, m + 1, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 1, n, l, r, pred);
}
};
struct Info {
int x;
};
Info operator+(const Info &a, const Info &b) {
return {std::min(a.x, b.x)};
}
// SegmentTree<Info> tree(m);
void solve() {
int n, k;
std::cin >> n >> k;
std::vector<int> a(n + 1);
SegmentTree<Info> tree(n);
int mn = 1e9;
for(int i = 1; i <= n; i++){
std::cin >> a[i];
tree.modify(i, {a[i]});
mn = std::min(mn, a[i]);
}
int g = 0;
for(int i = 2; i <= n; i++){
g = std::gcd(g, std::abs(a[i] - a[i - 1]));
}
if (g == 0){
std::cout << k << " " << 1LL * k * (k + 1) / 2 << "\n";
return;
}
std::vector<int> st;
for(int i = 1; i * i <= g; i++){
if (g % i == 0){
int j = g / i;
if (i > mn){
st.push_back(i - mn);
}
if (j > mn){
st.push_back(j - mn);
}
}
}
std::vector dp(n + 1, std::vector (20, 0));
for(int i = 1; i < n; i++){
dp[i][0] = std::abs(a[i] - a[i + 1]);
}
for(int j = 1; j < 20; j++){
for(int i = 1; i <= n; i++){
dp[i][j] = std::gcd(dp[i][j - 1], dp[((i + (1 << (j - 1))) > n ? n : (i + (1 << (j - 1))))][j - 1]);
}
}
for(int i = 1; i <= n; i++){
int l = 1, r = i - 1, L = 1;
while(l <= r){
int m = (l + r) / 2;
if (tree.rangeQuery(m, i).x < a[i]){
L = m + 1;
l = m + 1;
}else{
r = m - 1;
}
}
l = i + 1, r = n;
int R = n;
while(l <= r){
int m = (l + r) / 2;
if (tree.rangeQuery(i, m).x < a[i]){
R = m - 1;
r = m - 1;
}else{
l = m + 1;
}
}
int G = 0;
// std::cout << L << " " << R << "a\n";
for(int i = 19; i >= 0; i--){
if (L + (1 << i) <= R){
G = std::gcd(G, dp[L][i]);
L += 1 << i;
}
}
// std::cout << i << " " << G << "b\n";
if (G == 0){
continue;
}
std::vector<int> nst;
for(auto j : st){
if (G % (a[i] + j) == 0){
nst.push_back(j);
}
}
std::swap(st, nst);
}
i64 ans = 0;
for(auto x : st){
ans += x;
}
std::cout << st.size() << " " << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cout << std::fixed << std::setprecision(10); // 固定输出精度
int t = 1;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3448kb
input:
3 5 10 7 79 1 7 1 2 1000000000 1 2 1 100 1000000000
output:
3 8 0 0 100 5050
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
4 201 1000000000 1 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5 2 5...
output:
0 0 0 0 0 0 0 0
result:
ok 4 lines
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 3572kb
input:
500 4 1000000000 8 14 24 18 4 1000000000 17 10 18 14 4 1000000000 6 17 19 19 4 1000000000 15 14 15 25 4 1000000000 16 16 5 25 4 1000000000 4 30 20 5 4 1000000000 11 4 23 9 4 1000000000 14 25 13 2 4 1000000000 18 18 1 15 4 1000000000 22 22 22 28 4 1000000000 15 17 17 10 4 1000000000 22 14 13 25 4 100...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 78th lines differ - expected: '2 4', found: '3 5'