QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#793679 | #9738. Make It Divisible | shinonomezhou | RE | 1ms | 4672kb | C++23 | 1.7kb | 2024-11-29 22:37:22 | 2024-11-29 22:37:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e4+2e2;
vector<ll>b(N,0),ls(N,0),rs(N,0),g(N,0);
void dfs(ll now){
ll gg=0;
if(ls[now]){
dfs(ls[now]);
gg=__gcd(b[ls[now]]-b[now],g[ls[now]]);
}
ll g2=0;
if(rs[now]){
dfs(rs[now]);
g2=__gcd(b[rs[now]]-b[now],g[rs[now]]);
}
g[now]=__gcd(gg,g2);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int tt;
cin>>tt;
while(tt--){
ll n,k;
cin>>n>>k;
fill(ls.begin(),ls.begin()+n+10,0);
fill(rs.begin(),rs.begin()+n+10,0);
fill(g.begin(),g.begin()+n+10,0);
fill(b.begin(),b.begin()+n+10,0);
ll root=0;
for(int i=1;i<=n;i++){
cin>>b[i];
}
vector<ll>sk;
sk.push_back(0);
sk.push_back(1);
for(int i=2;i<=n;i++){
while(sk.size()){
ll x=sk.back();
if(x==0){
sk.push_back(i);
break;
}
if(b[i]<b[x]){
rs[x]=ls[i];
ls[i]=x;
sk.pop_back();
}else{
rs[x]=i;
sk.push_back(i);
break;
}
}
}
root=sk[1];
// for(int i=1;i<=n;i++)cout<<ls[i]<<'y'<<rs[i]<<'\n';
dfs(root);
// for(int i=1;i<=n;i++)cout<<g[i]<<'\n';
if(g[root]==0){
cout<<k<<' '<<1ll*k*(k+1)/2<<'\n';
continue;
}
set<ll>ans;
ll x=g[root];
for(ll i=1;i<=sqrt(x);i++){
if(x%i==0){
ll num=i-b[root];
if(num>=1&&num<=k)ans.insert(num);
num=x/i-b[root];
if(i*i!=x){
if(num>=1&&num<=k)ans.insert(num);
}
}
}
for(int i=1;i<=n;i++){
if(g[i]==0||i==root)continue;
for(auto it:ans){
ll num=it+b[i];
if(g[i]%num!=0)ans.erase(it);
}
}
ll cnt=0,sum=0;
for(auto it:ans){
cnt++;sum+=it;
}
cout<<cnt<<' '<<sum<<'\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4672kb
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: -100
Runtime Error
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...