QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#786446 | #9738. Make It Divisible | shengZzz# | ML | 0ms | 3724kb | C++20 | 1.7kb | 2024-11-26 21:34:50 | 2024-11-26 21:34:51 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5e4+10;
int a[N],n,m;
vector<int> X,dc;
inline int Gcd(int a,int b) {
return !b?a:gcd(b,a%b);
}
void check(int x) {
int T=sqrt(x);
for(int i=1;i<=T;i++) {
if(x%i==0) {
dc.push_back(i);
if(i!=x/i) dc.push_back(x/i);
}
}
}
int tot; ll sum;
int st[N],top,lc[N],rc[N],rt,Gd[N];
int build() {
for(int i=1;i<=n;i++) {
while(top && a[st[top]]>a[i]) lc[i]=st[top--];
if(top) rc[st[top]]=i;
st[++top]=i;
}
return st[1];
}
int TTT;
bool dfs(int p) {
int x=lc[p],y=rc[p];
int G=0;
if(x) {
if(!dfs(x)) return false;
G=gcd(Gd[x],G);
}
if(y) {
if(!dfs(y)) return false;
G=gcd(Gd[y],G);
}
int tt=a[p]+TTT;
if(G%tt) return false;
Gd[p]=gcd(G,tt);
return true;
}
bool ck(int t) {
TTT=t;
if(dfs(rt)) return true;
else return false;
}
void slv (int a,int b) {
if(a>b) swap(a,b);
check(b-a);
for(auto x:dc) {
if(x>a && x-a<=m) X.push_back(x-a);
}
for(auto x:X)
if(ck(x)) tot++,sum+=x;
cout<<tot<<" "<<sum<<"\n";
}
void solve() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
rt=build();
if(n==1) {
cout<<m<<" "<<(1ll)*m*(m+1)/2<<"\n";
return;
}
for(int i=1;i<n;i++) {
if(a[i]!=a[i+1]) { slv(a[i],a[i+1]); return;}
}
cout<<"0 0\n";
}
void init() {
X.clear(); dc.clear();
tot=0; sum=0; top=0;
}
int main() {
int T; scanf("%d",&T);
while(T--) {
init(); solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3724kb
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
Memory Limit Exceeded
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...