A pair of positive integers ( a , b ) (a,b) ( a , b ) is called special if ⌊ a b ⌋ = a m o d b \left\lfloor\frac{a}{b}\right\rfloor=a \bmod b ⌊ b a ⌋ = a mod b . Here, ⌊ a b ⌋ \left\lfloor\frac{a}{b}\right\rfloor ⌊ b a ⌋ is the result of the integer division between a a a and b b b , while a m o d b a \bmod b a mod b is its remainder.
You are given two integers x x x and y y y . Find the number of special pairs ( a , b ) (a,b) ( a , b ) such that 1 ≤ a ≤ x 1≤a≤x 1 ≤ a ≤ x and 1 ≤ b ≤ y 1≤b≤y 1 ≤ b ≤ y .
The first line contains a single integer t t t (1 ≤ t ≤ 100 1≤t≤100 1 ≤ t ≤ 100 ) — the number of test cases.
The only line of the description of each test case contains two integers x x x , y y y (1 ≤ x , y ≤ 1 0 9 1≤x,y≤10^9 1 ≤ x , y ≤ 1 0 9 ).
Output
For each test case print the answer on a single line.
Example
1 2 3 4 5 6 7 8 9 10 9 3 4 2 100 4 3 50 3 12 4 69 420 12345 6789 123456 789 12345678 9
output
1 2 3 4 5 6 7 8 9 1 0 2 3 5 141 53384 160909 36
Note
In the first test case, the only special pair is ( 3 , 2 ) (3,2) ( 3 , 2 ) .
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: ( 3 , 2 ) (3,2) ( 3 , 2 ) and ( 4 , 3 ) (4,3) ( 4 , 3 ) .
Idea
令 a = k b + m a=kb+m a = kb + m ,有 k , m ∈ N k,m\in\mathbb N k , m ∈ N ,且 k ≥ 0 , 0 ≤ m < b k\geq 0,0\leq m<b k ≥ 0 , 0 ≤ m < b ;那么 ⌊ a b ⌋ = k = m \left\lfloor\frac{a}{b}\right\rfloor=k=m ⌊ b a ⌋ = k = m ,将 k k k 用 m m m 代替,得到 a = ( b + 1 ) m a=(b+1)m a = ( b + 1 ) m ;可以看到,m = 0 m=0 m = 0 是不可行的。
首先枚举 b b b ,已知 1 ≤ b ≤ y 1\leq b\leq y 1 ≤ b ≤ y 。当 b b b 确定时,枚举 m m m ,可得 a a a ;已知 1 ≤ a ≤ x 1\leq a\leq x 1 ≤ a ≤ x ,故得到不等式组 1 ≤ m < b 1\leq m<b 1 ≤ m < b 和 1 ≤ ( b + 1 ) m ≤ x 1\leq (b+1)m\leq x 1 ≤ ( b + 1 ) m ≤ x 。
显然,m m m 的上限值为 min ( ⌊ x b + 1 ⌋ , b − 1 ) \min\left(\left\lfloor\frac{x}{b+1}\right\rfloor,b-1\right) min ( ⌊ b + 1 x ⌋ , b − 1 ) 。
综上所述,答案为 ∑ b = 1 y min ( ⌊ x b + 1 ⌋ , b − 1 ) \sum\limits_{b=1}^y\min\left(\left\lfloor\frac{x}{b+1}\right\rfloor,b-1\right) b = 1 ∑ y min ( ⌊ b + 1 x ⌋ , b − 1 ) 。
令 x b + 1 ≤ b − 1 \frac{x}{b+1}\le b-1 b + 1 x ≤ b − 1 ,得到 b ≥ x + 1 b\ge\sqrt{x+1} b ≥ x + 1 ;可知 x + 1 \sqrt{x+1} x + 1 附近有 B B B ,是满足 ⌊ x b + 1 ⌋ ≤ b − 1 \left\lfloor\frac{x}{b+1}\right\rfloor\le b-1 ⌊ b + 1 x ⌋ ≤ b − 1 最小的 b b b 。
$\begin{aligned}\text{answer}&=(0+1+2\cdots +B-2)+\left(\left\lfloor\frac{x}{B+1}\right\rfloor+\left\lfloor\frac{x}{B+2}\right\rfloor+\cdots+\left\lfloor\frac{x}{y+1}\right\rfloor\right)\\&=\sum\limits_{i=1}^{B-2}i+\sum\limits_{i=B+1}^{y+1}\left\lfloor\frac{x}{i}\right\rfloor\end{aligned}$
求等差数列和数论分块。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <cmath> #include <iostream> using namespace std;typedef long long ll;const int N=100005 ;int _;int main () { for (cin>>_;_;_--){ ll x,y; cin>>x>>y; ll B=sqrt (x+1 ); while (x/(B+1 )>B-1 ){ B++; } ll ans=0 ; if (B-1 >y){ ans+=y*(y-1 )/2 ; }else { ans+=(B-1 )*(B-2 )/2 ; } for (ll l=B+1 ,r;l<=y+1 ;l=r+1 ){ if (l>x){ break ; } r=min (x/(x/l),y+1 ); ans+=(x/l)*(r-l+1 ); } cout<<ans<<endl; } return 0 ; }