QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#382784 | #6440. Xingqiu's Joke | shstyle | WA | 1242ms | 3872kb | C++23 | 1.7kb | 2024-04-08 19:06:13 | 2024-04-08 19:06:14 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
typedef pair<int,int> PII;
int n,m,k;
vector<int> p;
map<int,int> mp;
//unordered_map<int,> mp;
bool tf[N];
vector<PII> ys;
int ans;
void dfs(int x,int y,int num){
// cout<<x<<" "<<y<<" "<<num<<endl;
ans=min(ans,num+min(x,y)-1);
if(x==1||y==1){
return;
}
int dist=y-x;
// cout<<"--"<<x<<" "<<y<<endl;
for(auto [u,v]:ys){
// cout<<dist<<" "<<u<<endl;
if(dist%u!=0) continue;
int tt=x%u;
int num1=x-tt,num2=y-tt;
int gg=u;
int res=num+tt+mp[gg];
// cout<<num1<<" "<<num2<<" "<<res<<" "<<tt<<" "<<mp[gg]<<endl;
if(res<ans){
dfs(num1/gg,num2/gg,res);
}
tt=u-tt;
num1=x+tt,num2=y+tt;
gg=gcd(num1,num2);
res=num+tt+mp[gg];
if(res<ans){
dfs(num1/gg,num2/gg,res);
}
}
}
void dfs1(int x,int sum,int cnt){
if(x>=ys.size()){
mp[sum]=cnt;
return;
}
int res=1,cc=0;
for(int i=0;i<=ys[x].second;i++,res*=ys[x].first){
dfs1(x+1,sum*res,cnt+i);
}
}
void __(){
cin>>n>>m;
ans=min(n,m)-1;
if(n>m) swap(n,m);
int cha=m-n;
ys.clear();
mp.clear();
for(int i=0;p[i]<=(m-n)/p[i];i++){
int cnt=0;
while(cha%p[i]==0){
cnt++;
cha/=p[i];
}
if(cnt!=0)
ys.push_back({p[i],cnt});
}
if(cha!=1){
ys.push_back({cha,1});
}
dfs1(0,1,0);
dfs(min(n,m),max(n,m),0);
// printf("%lld\n",ans);
cout<<ans<<"\n";
}
int main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
for(int i=2;i<=100000;i++){
if(!tf[i]) p.push_back(i);
for(int j=0;p[j]<=100000/i;j++){
tf[i*p[j]]=1;
if(i%p[j]==0) break;
}
}
int _;
cin>>_;
while(_--){
__();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3872kb
input:
5 4 7 9 8 32 84 11 35 2 1
output:
2 7 5 4 0
result:
ok 5 number(s): "2 7 5 4 0"
Test #2:
score: -100
Wrong Answer
time: 1242ms
memory: 3744kb
input:
300 223528529 446621399 755625805 978718675 257717538 480810408 277875777 54782907 963091558 739998688 282469431 505562301 38752451 261845321 474429597 697522467 67026335 290119205 874629705 651536835 301964126 78871256 316864911 93772041 545942587 322849717 640801578 417708708 683070559 906163429 9...
output:
14 20 15 17 16 17 14 19 17 17 15 15 18 17 17 19 16 19 15 18 17 19 20 15 17 17 18 12 20 18 12 19 16 15 15 19 18 18 17 17 14 17 14 16 15 15 20 20 18 14 18 17 16 18 16 16 17 14 19 18 16 19 21 19 17 16 18 16 23 17 15 20 18 20 16 16 17 17 12 18 20 17 16 18 17 15 20 12 15 18 16 12 19 20 15 18 17 14 15 16 ...
result:
wrong answer 1st numbers differ - expected: '12', found: '14'