QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#139399#6744. SquareZZQ323AC ✓69ms3484kbC++143.0kb2023-08-13 12:19:442023-08-13 12:19:46

Judging History

你现在查看的是最新测评结果

  • [2023-08-13 12:19:46]
  • 评测
  • 测评结果:AC
  • 用时:69ms
  • 内存:3484kb
  • [2023-08-13 12:19:44]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define ll int64_t
#define pii pair<int ,int >
#define pllll pair<ll ,ll >
#define p(x,y) pair<x,y>
#define mkp make_pair

//得到坐标,但是并不是矩形坐标,而是数字三角形的坐标
/*
pllll getcoord(ll z)
{
    ll x=0;
    //这个写法是什么,倍增倍减吗?
    //二进制优化,基的概念去表示一个数字
    for(int i=1<<30;i;i>>=1){
        if( (x|i)*((x|i)+1)/2 <= z )
            x|=i;
    }
    //x是指行数
    return {x,z-x*(x+1)/2};
}

void solve()
{
    int64_t x, y;
    cin >> x >> y;
    if (x > y) {
        cout << x - y << '\n';
        return;
    }

    auto [sx, sy] = getCoord(x - 1);
    auto [tx, ty] = getCoord(y - 1);
    int64_t ans = 1e18;

    debug(sx, sy);
    debug(tx, ty);
    assert (sx <= tx);
    if (tx - ty >= sx - sy) {
        ans = min(ans, tx - sx + ((tx - ty) - (sx - sy)));
    }
    {
        ans = min(ans, tx - sx + 1 + (tx - ty) + (sy + 1));
    }

    cout << ans << '\n';
}


*/


ll mysqrt(ll x){ll s=max(0ll,(ll)sqrt(x)-3ll);while( (s+1)*(s+1)<=x )++s;return s;}
ll upstep(ll x){return (mysqrt(x<<3)+3)/2;}
ll mmul(ll a,ll b){if ((double)a * b > 6e18)return 6e18;else return a*b;}
ll jump(ll x,ll cnt){ll s=upstep(x);return x+mmul(cnt,s+s+cnt-1)/2;}
ll  prev_crit(ll x)
{
    ll n=mysqrt(x<<1)+5;
    while(n*(n+1)/2+1 > x)--n;
    return n*(n+1)/2+1;
}

signed main(int argc,char** argv)
{
    ios::sync_with_stdio(false);

    int T;
    cin>>T;
    while(T--)
    {
        ll x,y;
        cin>>x>>y;
        if(x>=y){
            cout<<x-y<<'\n';
            continue;;
        }
        ll r=1;
        while(jump(x,r) < y)r<<=1;
        ll l=0;
        while(r-l>1)
        {
            ll mid=l+r>>1;
            if( jump(x,mid) < y )l=mid;
            else r=mid;
        }

        ll ss=r;
        ll ans=0;
        while(1)
        {
            //目标和左端点相差多远
            ll diff=jump(x,ss)-y;
            //压根没差,直接中奖
            if(!diff)break;
            //左端点的数值pre
            ll pre=prev_crit(x);
            if(diff<=x-pre)
            {//如果还能待在这里层数
                x-=diff;
                ans+=diff;
                break;
            }
            ans+=x-pre;
            //x调整为左端点,继续跳步
            x=pre;
            //目标和左端点相差多远
            diff = jump(x,ss)-y;
            //压根没差,直接中奖
            if(!diff)break;
            //如果x跳步后和目标多出来的的数值还比跳的次数多
            //那就选择减小,这样更优
            if(ss+1<=diff)
            {
                --x;
                ++ans;
                continue;
            }
            //否则如果贸然减小的话造成的影响会
            ans+=diff;
            break;
        }
        cout<<ans+ss<<'\n';
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3420kb

input:

2
5 1
1 5

output:

4
3

result:

ok 2 number(s): "4 3"

Test #2:

score: 0
Accepted
time: 69ms
memory: 3484kb

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