QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#766241 | #9738. Make It Divisible | i_wish_a_girl_friend | WA | 2ms | 11680kb | C++23 | 3.3kb | 2024-11-20 16:43:41 | 2024-11-20 16:43:42 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
#define pii pair<int,int>
using namespace std;
//const int mod=998244353;
int gcd(int a,int b){return b?gcd(b,a%b):a;};
int qpw(int a,int b,int mod){int ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
//int inv(int x){return qpw(x,mod-2);}
const int N=1e7+10;
int sta[N];
int rs_min[N],ls_min[N];
int rs_max[N],ls_max[N];
int root_min,root_max;
int query_min(int l,int r){
int x=root_min;
for(;;x=(r<x?ls_min[x]:rs_min[x]))if(l<=x&&x<=r)return x;
}
int query_max(int l,int r){
int x=root_max;
for(;;x=(r<x?ls_max[x]:rs_max[x]))if(l<=x&&x<=r)return x;
}
void solve(){
int n,k;cin>>n>>k;
vector<int>a(n+1);
for(int i=1,x;i<=n;i++)cin>>a[i];
function<void()>build_min=[&](){
int top=0,cur=0;
for(int i=1;i<=n;i++){
cur=top;
while(cur&&a[sta[cur]]>=a[i])cur--;
if(cur)rs_min[sta[cur]]=i;
if(cur<top)ls_min[i]=sta[cur+1];
sta[++cur]=i;
top=cur;
}
};
function<void()>build_max=[&](){
int top=0,cur=0;
for(int i=1;i<=n;i++){
cur=top;
while(cur&&a[sta[cur]]<=a[i])cur--;
if(cur)rs_max[sta[cur]]=i;
if(cur<top)ls_max[i]=sta[cur+1];
sta[++cur]=i;
top=cur;
}
};
build_min();
root_min=sta[1];
build_max();
root_max=sta[1];
int flag=0;
int ok=0;
vector<int>p;
function<void(int,int)>dfs=[&](int l,int r){
if(l<1||r>n)return;
if(l>=r)return;
if(!flag){
flag=1;
int mx_id=query_max(l,r);
int mi_id=query_min(l,r);
if(a[mx_id]==a[mi_id]){
ok=1;
return;
}
int now=a[mx_id]-a[mi_id];
int t=a[mi_id];
for(int i=1;i<=sqrt(now);i++){
if(now%i==0){
if(now/i-t>=1&&now/i-t<=k)p.pb(now/i-t);
if(i-t>=1&&i-t<=k)p.pb(i-t);
}
}
// cout<<mi_id<<"\n";
dfs(l,mi_id-1);
dfs(mi_id+1,r);
}
else {
int mx_id=query_max(l,r);
int mi_id=query_min(l,r);
int mi=a[mi_id];
int mx=a[mx_id];
if(mi==mx)return;
vector<int>res;
// cout<<l<<" "<<r<<'\n';
// cout<<mi<<" "<<mx<<'\n';
for(int x:p){
if((mi-mx)%(x+mi)==0)res.pb(x);
}
int sum=0;
p=res;
// cout<<"P:";
// for(int x:p)sum+=x,cout<<x<<" ";
// cout<<'\n';
// cout<<p.size()<<" "<<sum<<'\n';
dfs(l,mi_id-1);
dfs(mi_id+1,r);
}
};
if(ok){
cout<<n<<" "<<n*(n+1)/2<<'\n';
return;
}
// cout<<query_max(1,n)<<'\n';
dfs(1,n);
int sum=0;
for(int x:p)sum+=x;
cout<<p.size()<<" "<<sum<<'\n';
}
signed main(){
ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
int _=1;
cin>>_;
while(_--)solve();
}
/*
1
5 10
7 79 1 7 1
*/
详细
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 11680kb
input:
3 5 10 7 79 1 7 1 2 1000000000 1 2 1 100 1000000000
output:
3 8 0 0 0 0
result:
wrong answer 3rd lines differ - expected: '100 5050', found: '0 0'