1. 程式人生 > 其它 >C. Floor and Mod ——(Codeforces Round #701 (Div. 2))

C. Floor and Mod ——(Codeforces Round #701 (Div. 2))

C. Floor and Mod

https://codeforces.com/contest/1485/problem/C

題意

給出x和y,求出滿足 \(1<=a<=x, 1<=b<=y, a / b = a mod b\) 條件的\((a,b)\)數量(此處 \(/\) 指的是向下取整,下同).

思路

\(k = a / b\), 則有 \(a = k * b + k\), 即: \(a = k * (b + 1)\),所以 \(b = a / k - 1\).
因為 \(k < b\), 所以 \(a > k * (k + 1)\),即: \(k < {sqrt}(a)\)

,
所以我們只需要列舉 \(k(1 <= k <= {sqrt}(x))\) ,對於每一個 \(k\):

對應的b最小值為 \(k + 1\), 因為 \(b > k\).
對應的b的最大值為 \(min(y, x / k - 1)\) ,因為 \(a\)的上限為 \(x\).
所以對於當前k來說,對答案的貢獻為 \(min(y, x/ k - 1) - k\).

所以列舉k即可得出答案。

Code

/*---------------------------------------------------------------
            ∧_∧
      ∧_∧  (´<_` )  
     ( ´_ゝ`) /  ⌒i     
    /   \     | |
    /   / ̄ ̄ ̄ ̄/  |
  __(__ニつ/     _/ .| .|____
     \/____/ (u ⊃
--------------------------------------------------------------*/
#include<bits/stdc++.h>
#define IO  ios::sync_with_stdio(false);cin.tie(0)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define req(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back		
#define fi first
#define se second
#define PI pair<int,int>
#define PII pair<ll,PI>

using namespace std;

typedef long long ll;
const int N=1e5+7;
const ll mod=1e9+7;

int main()
{
	IO; 
	int t;
	cin>>t;
	while(t--){
		ll x,y;
	    cin>>x>>y;
	    ll ans=0,k=1;
	    while(k<=y&&k*(k+1)<=x){
	        ans+=min(y,x/k-1)-k;
	        k++;
	    }cout<<ans<<endl;
	}
	return 0;
} 
Code will change the world !