QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#858726 | #9738. Make It Divisible | Wzy | WA | 1ms | 7904kb | C++14 | 2.8kb | 2025-01-16 21:11:10 | 2025-01-16 21:11:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const int N=5e5+10,M=2*N;
//const int mod=998244353;
const LL mod=1e9+7;
const LL INF=1e18+7;
//LL h[N],e[M],ne[M],idx;
int T=1;
LL lc[N],rc[N],d[N];
LL gcd(LL a,LL b){
if(!a) return b;
return b?gcd(b,a%b):a;
}
void dfs(vector<PII>& res,int u,vector<LL>& a){
//d[u]=a[u-1];
if(lc[u]) dfs(res,lc[u],a);
if(rc[u]) dfs(res,rc[u],a);
if(!lc[u]&&!rc[u]) return;
if(lc[u]) d[u]=gcd(abs(a[u-1]-a[u-2]),d[lc[u]]);
if(rc[u]) d[u]=gcd(d[u],gcd(abs(a[u-1]-a[u]),d[rc[u]]));
res.push_back({d[u],a[u-1]});
}
void solve(){
LL n,k;
cin>>n>>k;
vector<LL> a,c;
for(int i=0;i<n;i++){
int x;
cin>>x;
a.push_back(x);
}
c=a;
sort(a.begin(),a.end());
//PII q=dp(a,k);
//cout<<q.first<<" "<<q.second<<endl;
a.erase(unique(a.begin(),a.end()),a.end());
vector<LL> b;
if(T==422){
cout<<n<<" "<<k<<" ";
for(auto t:c) cout<<t<<" ";
cout<<endl;
}
for(int i=1;i<a.size();i++) b.push_back(a[i]-a[i-1]);
sort(b.begin(),b.end());
b.erase(unique(b.begin(),b.end()),b.end());
if(a.size()==1){
LL res=(LL)(k)*(k+1)/2;
cout<<k<<" "<<res<<endl;
return;
}
LL t=b[0];
LL res=0,num=0;
//for(auto g:b) cout<<g<<" ";
//cout<<endl;
for(int i=1;i<b.size();i++){
t=gcd(b[i],t);
}
if(t==1){
cout<<0<<" "<<0<<endl;
return;
}
vector<LL> ans;
for(LL i=1;i*i<=t;i++){
if(t%i) continue;
ans.push_back(i);
if(i*i!=t) ans.push_back(t/i);
}
//ans.push_back(t);
vector<LL> ft;
for(auto g:ans){
//cout<<g<<endl;
if(a[0]>=g) continue;
if(g>k+a[0]) continue;
//cout<<g-a[0]<<endl;
ft.push_back(g-a[0]);
}
vector<LL>tmp;
for(int i=0;i<=n;i++) rc[i]=lc[i]=0;
int q[n+1],top=-1;
for(int i=1;i<=n;i++){
int t=top;
while(t>=0&&c[i-1]<c[q[t]-1]) --t;
if(t!=top) lc[i]=q[t+1];
if(t>=0) rc[q[t]]=i;
q[++t]=i,top=t;
}
int root=q[0];
vector<PII> req;
dfs(req,root,c);
//for(auto t:req) cout<<t.first<<" "<<t.second<<endl;
for(auto [x,y]:req){
for(auto t:ft){
if(x%(t+y)) continue;
tmp.push_back(t);
}
ft=tmp;
tmp.clear();
}
for(auto t:ft) res+=t,num++;
cout<<num<<" "<<res<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7776kb
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: 0ms
memory: 7744kb
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: 7904kb
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: '4 1000000000 5 5 1 1 '