QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#790134 | #9738. Make It Divisible | wildfire032 | WA | 6ms | 21580kb | C++23 | 2.4kb | 2024-11-28 02:06:52 | 2024-11-28 02:06:53 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,l,r) for(int i=l;i<=r;++i)
using namespace std;
using ll =long long;
const int N=1e6+50;
const int mod=1e9+7;
#define int long long
#define pb push_back
ll a[N],b[N],n,rs[N],ls[N],sta[N],g[N],vis[N];
ll tp=0;
ll getmi(ll a,ll x)
{
ll rt=1;
while(x)
{
if(x&1) rt=rt*a%mod;
a=a*a%mod,x>>=1;
}
return rt;
}
void dfs(ll x)
{
if(ls[x]!=-1)dfs(ls[x]),g[x]=__gcd(g[x],g[ls[x]]);
if(rs[x]!=-1) dfs(rs[x]),g[x]=__gcd(g[x],g[rs[x]]);
}
bool check(ll x)
{
rep(i,1,n) g[i]=a[i]+x,vis[i]=0;
auto dfs = [&](auto&& self, int& x) -> void{
assert(vis[x]==0);
vis[x]=1;
if(ls[x]!=-1) self(self,ls[x]);
if(ls[x]!=-1)g[x]=__gcd(g[x],g[ls[x]]);
if(rs[x]!=-1) self(self,rs[x]);
if(rs[x]!=-1)g[x]=__gcd(g[x],g[rs[x]]);
return;
};
dfs(dfs,tp);
rep(i,1,n) if(g[i]%(a[i]+x)!=0) return false;
return true;
}
ll gcd(ll x,ll y)
{
if(!x) return y;
if(!y) return x;
return __gcd(x,y);
}
void solve()
{
ll k;
ll cnt=0,sum=0;
cin>>n>>k;
rep(i,1,n) cin>>a[i];
ll mn=a[1],gg=abs(a[2]-a[1]);
rep(i,1,n) mn=min(mn,a[i]);
ll fg=1;
rep(i,2,n)
{
if(a[i]!=a[1])
{
fg=0;gg=gcd(gg,abs(a[i]-a[i-1]));
}
//cout<<gg<<"\n";
}
if(fg) return cout<<k<<" "<<k*(k+1)/2<<"\n",void();
rep(i,1,n)
{
if(a[i]==mn)
{
tp=i;
}
}
int top=0,cur=0;
rep(i,1,n)sta[i]=0,b[i]=a[i],ls[i]=rs[i]=-1;
for(int i=1;i<=n;i++)
{
cur=top;
while(cur&&b[sta[cur]]>=b[i])
cur--;
if(cur)
rs[sta[cur]]=i;
if(cur<top)
ls[i]=sta[cur+1];
sta[++cur]=i;
top=cur;
}
//rep(i,1,n) cout<<ls[i]<<" "<<rs[i]<<"\n";
vector<int> ans;
for(int i=1;i*i<=gg;i++)
{
if(gg%i==0) ans.push_back(i);
if(gg!=i*i) ans.push_back(gg/i);
}
if(n==50000) cout<<"?";
for(auto u:ans)
{
//cout<<"!"<<u-mn<<"\n";
if(u-mn<=0||u-mn>k) continue;
if(check(u-mn))
{
cnt++;sum+=u-mn;
}
}
cout<<cnt<<" "<<sum<<"\n";
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t=1;
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: 15832kb
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: 2ms
memory: 15896kb
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: 0
Accepted
time: 0ms
memory: 15908kb
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:
ok 500 lines
Test #4:
score: -100
Wrong Answer
time: 6ms
memory: 21580kb
input:
1 50000 1000000000 230 286458 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 41263680 ...
output:
?0 0
result:
wrong answer 1st lines differ - expected: '0 0', found: '?0 0'