QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#367454 | #6744. Square | ikunsome | AC ✓ | 286ms | 3580kb | C++23 | 4.3kb | 2024-03-25 23:12:08 | 2024-03-25 23:12:08 |
Judging History
answer
#include<bits/stdc++.h>
#include<ext/rope>
#include<bits/extc++.h>
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>order_set;
//find_by_order() order_of_key()
const int maxn=2e3+5;
const int INF=0x3f3f3f3f;
const int inf=0x3f3f3f3f;
#define int long long
#define double long double
const int LINF=0x3f3f3f3f3f3f3f3f;
#define bit(x) (1LL << (x))
#define lowbit(x) (x & (-x))
#define sq(x) ((x) * (x))
#define pb(i) push_back(i)
#define mk(i,j) make_pair((i),(j))
#define pum(i,j) push(make_pair((i),(j)))
#define pbm(i,j) push_back(make_pair((i),(j)))
#define fup(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define fdown(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
typedef unsigned long long ull;
#define ull unsigned ll
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vp = vector<pair<int,int>>;
using vvi = vector<vector<int>>;
using vvd = vector<vector<double>>;
using vvp = vector<vector<pair<int,int>>>;
using vvc = vector<vector<char>>;
using mi = map<int,int>;
using mpi = map<pair<int,int>,int>;
using miv = map<int,vector<int>>;
using mpv = map<pair<int,int>,vi>;
using gi = greater<int>;
using gp = greater<pi>;
//using djs_queue =priority_queue<pi,vp,gp>;
//using ld = long double;
using namespace std;
double eps=1e-12;
double pai=acos(-1.0);
int mod=1e9+7;
//int get(){
// int x=0,f=1; char ch=getchar();
// while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
// while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
// return x*f;
//}
int gcd(int a, int b){
return b?gcd(b,a%b):a;
}
int qpow(int x,int y) {
int res=1;
while(y){
if(y&1)res=res*x%mod;
x=x*x%mod;
y>>=1;
}
return res;
}
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,-1,-1,1};
int nowx,nowy;
int check(__int128 x,__int128 y){
__int128 pos=sqrtl(2*x);
if((1+pos)*(pos)/2<x)pos++;
pos++;
__int128 temp=sqrtl(sq(2*pos-1)+8*(y-x));
__int128 last=(-(2*pos-1)+temp)/2;
if(x+(pos+pos+last-1)*(last)/2<y)last++;
return (int)x+(pos+pos+last-1)*(last)/2-y+last;
}
void init(int x,int y){
__int128 pos=sqrtl(2*x);
if((1+pos)*(pos)/2<x){
nowx=pos;
}
else nowx=pos-1;
// cout<<nowx<<'\n';
}
void solve(){
int x,y;cin>>x>>y;
if(x>=y){
cout<<x-y<<'\n';
return;
}
int ans=LINF;
fup(i,max(1ll,x-100),x,1){
ans=min(ans,x-i+check(i,y));
}
init(x,y);
// cout<<nowx<<'\n';
if(x>100)ans=min(ans,check((1+nowx)*(nowx)/2,y)+x-(1+nowx)*(nowx)/2);
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout<<fixed<<setprecision(20);
// clock_t tStart=clock();
//SetIO("main");
// freopen("111.in","r",stdin);
// freopen("out.txt","w",stdout);
int T = 1;
cin>>T;
while(T--){
solve();
}
//cerr<<"\nTime Taken: ";
//cerr<<fixed<<setprecision(0)<<(double)(clock() - tStart)/CLOCKS_PER_SEC;
}
/*
*/
//# ┏┓ ┏┓+ +
//# ┏┛┻━━━┛┻┓ + +
//# ┃ ┃
//# ┃ ━ ┃ ++ + + +
//# ████━████ ┃+
//# ┃ ┃ +
//# ┃ ┻ ┃
//# ┃ ┃ + +
//# ┗━┓ ┏━┛
//# ┃ ┃
//# ┃ ┃ + + + +
//# ┃ ┃
//# ┃ ┃ + gmy保佑,一发过,不能再痒了
//# ┃ ┃
//# ┃ ┃ +
//# ┃ ┗━━━┓ + +
//# ┃ ┣┓
//# ┃ ┏┛
//# ┗┓┓┏━┳┓┏┛ + + + +
//# ┃┫┫ ┃┫┫
//# ┗┻┛ ┗┻┛+ + + +
//————————————————————————————————
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3536kb
input:
2 5 1 1 5
output:
4 3
result:
ok 2 number(s): "4 3"
Test #2:
score: 0
Accepted
time: 286ms
memory: 3580kb
input:
100000 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 ...
output:
0 2 1 4 3 2 6 5 4 3 8 7 6 5 4 10 9 8 7 6 5 12 11 10 9 8 7 6 14 13 12 11 10 9 8 7 16 15 14 13 12 11 10 9 8 18 17 16 15 14 13 12 11 10 9 20 19 18 17 16 15 14 13 12 11 10 22 21 20 19 18 17 16 15 14 13 12 11 24 23 22 21 20 19 18 17 16 15 14 13 12 26 25 24 23 22 21 20 19 18 1 0 2 2 1 3 4 3 2 4 6 5 4 3 5 ...
result:
ok 100000 numbers