2 条题解

  • 1
    @ 2025-11-28 13:39:20

    补充一个c++题解(b*b仍是int类型,这里若定义b为int会溢出)

    #include<bits/stdc++.h>
    using namespace std;
    long long max(long long x,long long y){
    	if(x>=y)return x;
    	return y;
    }
    int main()
    {
    	int d;
    	cin>>d;
    	while(d--){
    		long long a,b;
    		cin>>a>>b;
    		long long t=abs(b*b-a*a);long long ans=0;
    		ans+=max(t/4-1,0);
    		ans+=(t-1)/2;
    		cout<<ans<<endl;
    	}
    	
    }
    
    • 0
      @ 2025-10-13 21:09:40

      无尽的梯子

      通过枚举可发现,相邻两数差值可涵盖除1外的所有奇数,而相隔一个数的差值涵盖除4以外所有4的倍数,之后相差更多的元素差值都为上述两种情况的子集,所以只要数目标(|a^2-b^2|)前有几个奇数和几个4的倍数即可

      参考代码(python)

      from sys import stdin,setrecursionlimit
      from math import inf,ceil,sqrt
      from collections import Counter,deque
      
      for _ in range(int(stdin.readline())):
          a,b=[int(_) for _ in stdin.readline().split()]
          d=abs(a*a-b*b)
          ans=(d-1)//2+max(d//4-1,0)
          print(ans)
      
      
      • 1

      [2025 实验室一面 && 牛客多校] 无尽的梯子

      信息

      ID
      1095
      时间
      2000ms
      内存
      512MiB
      难度
      8
      标签
      递交数
      80
      已通过
      15
      上传者