QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#785448 | #9738. Make It Divisible | cbdsopa | WA | 3ms | 48152kb | C++14 | 5.1kb | 2024-11-26 17:59:04 | 2024-11-26 17:59:04 |
Judging History
answer
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define db double
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define sky fflush(stdout)
#define pc putchar
namespace IO{
const int lim = 1 << 20;
char buf[lim + 3], *p1 = buf, *p2 = buf;
inline char gc(){
if(p1 == p2) p2 = (p1 = buf) + fread(buf,1,lim,stdin);
return p1 == p2 ? EOF : *p1++;
}
template<class T>
inline void read(T &s){
s = 0;char ch = gc();bool f = 0;
while(ch < '0' || '9'<ch) {if(ch == '-') f = 1; ch = gc();}
while('0'<=ch && ch<='9') {s = s * 10 + (ch ^ 48); ch = gc();}
if(ch == '.'){
T p = 0.1;ch = gc();
while('0' <= ch && ch <= '9') {s = s + p * (ch ^ 48);p /= 10;ch = gc();}
}
s = f ? -s : s;
}
template<class T,class ...A>
inline void read(T &s,A &...a){
read(s); read(a...);
}
template<class T>
inline void print(T x){
if(x<0) {x = -x; pc('-');}
static char st[40];
static int top;
top = 0;
do{st[++top] = x - x / 10 * 10 + '0';} while(x /= 10);
while(top) {pc(st[top--]);}
}
template<class T,class ...A>
inline void print(T s,A ...a){
print(s); print(a...);
}
};
using IO::read;
using IO::print;
const int N = 5e4;
int n, m;
int a[N + 3];
int gcd(int a, int b){
if(!b) return a;
return gcd(b, a % b);
}
struct SegTree{
struct node{
int Gcd;
}t[N * 4 + 3];
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
void pushup(int x){
t[x].Gcd = gcd(t[lc(x)].Gcd, t[rc(x)].Gcd);
}
void build(int x, int l, int r){
if(l == r){
t[x].Gcd = a[l] - a[l - 1];
return;
}
int mid = l + r >> 1;
build(lc(x), l, mid);
build(rc(x), mid + 1, r);
pushup(x);
}
int query(int x, int l, int r, int L, int R){
if(L > R) return 0;
if(L <= l && r <= R){
return t[x].Gcd;
}
int mid = l + r >> 1;
int res = 0;
if(L <= mid) res = gcd(res, query(lc(x), l, mid, L, R) );
if(mid + 1 <= R) res = gcd(res, query(rc(x), mid + 1, r, L, R) );
return res;
}
}t;
const int S=199;
struct HashMap{
std::vector<int>head,nxt;
struct node{
int x,v;
};std::vector<node>to;
inline void clear(){
head.resize(S+3,-1);
}
HashMap(){
clear();
}
inline int & operator [] (int x){
int u = x % S;
for(int i = head[u]; ~i; i = nxt[i]){
if(to[i].x == x){
return to[i].v;
}
}
nxt.push_back(head[u]);
head[u] = to.size();
to.push_back({x, 0});
return to[to.size() - 1].v;
}
}s[N + 3];
int ch[N + 3][2];
int sta[N + 3], top;
int rt;
//std::unordered_set<int>s[N + 3];
int l[N + 3], r[N + 3];
std::vector<std::pair<int, int> >tmp1;
std::vector<int>tmp2;
bool any[N + 3];
void calc(int x, int t, int sum){
if(t == tmp1.size() ){
tmp2.push_back(sum);
return;
}
for(int i = 0; i <= tmp1[t].second; ++i){
calc(x, t + 1, sum);
sum *= tmp1[t].first;
}
}
void dfs(int x){
l[x] = r[x] = x;
if(!ch[x][0] && !ch[x][1]){
any[x] = 1;
return;
}
for(int i = 0; i < 2; ++i){
if(ch[x][i]){
dfs(ch[x][i]);
l[x] = std::min(l[x], l[ch[x][i] ]);
r[x] = std::max(r[x], r[ch[x][i] ]);
}
}
int g = t.query(1, 1, n, l[x] + 1, r[x]);
if(g == 0){
any[x] = any[ch[x][0] ] & any[ch[x][1] ];
if(ch[x][0] && ch[x][1]){
int y1 = ch[x][0], y2 = ch[x][1];
for(auto it : s[y1].to){
if(s[y2][it.x] == 1){
s[x][it.x] = 1;
}
}
}else{
int y = ch[x][0] | ch[x][1];
s[x] = s[y];
}
return;
}
int k = abs(g);
tmp1.clear();
tmp2.clear();
for(int i = 2; i * i <= k; ++i){
if(k % i == 0){
int cnt = 0;
while(k % i == 0) k /= i, ++cnt;
tmp1.push_back({i, cnt});
}
}
if(k != 1) tmp1.push_back({k, 1});
//fprintf(stderr, "%d -> %d %d [%d, %d] %d\n", x, ch[x][0], ch[x][1], l[x], r[x], g);
calc(x, 0, 1);
for(auto it : tmp2){
if(it - a[x] < 1 || it - a[x] > m) continue;
//fprintf(stderr, "%d(%d) ", it - a[x], it);
if((a[l[x] ] + it - a[x]) % it == 0){
bool flag = 1;
if(!any[ch[x][0] ])
if(s[ch[x][0] ][it - a[x] ] == 1)
flag = 0;
if(!any[ch[x][1] ])
if(s[ch[x][1] ][it - a[x] ] == 1)
flag = 0;
if(flag) s[x][it - a[x] ] = 1;
//fprintf(stderr, "<-(%d) ", flag);
}
}
/*
fprintf(stderr,"\n");
for(auto it : s[x]){
fprintf(stderr, "%d ",it);
}fprintf(stderr, "\n");
*/
}
inline void solve(){
read(n, m);
any[0] = 1;
for(int i = 1; i <= n; ++i){
read(a[i]);
s[i].clear();
ch[i][0] = ch[i][1] = 0;
any[i] = 0;
}
t.build(1, 1, n);
top = 0;
sta[++top] = 1;
for(int i = 2; i <= n; ++i){
int k = top;
while(k && a[sta[k] ] > a[i])
--k;
if(k < top) ch[i][0] = sta[k + 1];
if(k) ch[sta[k] ][1] = i;
sta[++k] = i;
top = k;
}
rt = sta[1];
dfs(rt);
bool flag = 1;
for(int i = 1; i <= n; ++i){
if(!any[i]) flag = 0;
}
if(flag){
printf("%d %lld\n", m, 1ll * (m + 1) * m / 2);
return;
}
ll sum = 0;
for(auto it :s[rt].to){
sum += it.x;
}
printf("%d %lld\n", s[rt].to.size(), sum);
fprintf(stderr, "%dms\n", clock() );
}
int main(){
#ifdef LOCAL
file(a);
#endif
int T; read(T);
while(T--){
solve();
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 48152kb
input:
3 5 10 7 79 1 7 1 2 1000000000 1 2 1 100 1000000000
output:
0 0 3 8 100 5050
result:
wrong answer 1st lines differ - expected: '3 8', found: '0 0'