首页 > 其他分享 >D2. Reverse Card (Hard Version)

D2. Reverse Card (Hard Version)

时间:2024-05-02 19:12:00浏览次数:13  
标签:le gcd cdot Hard Version right test D2 left

D2. Reverse Card (Hard Version)

The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.

You are given two positive integers $n$, $m$.

Calculate the number of ordered pairs $(a, b)$ satisfying the following conditions:

  • $1\le a\le n$, $1\le b\le m$;
  • $b \cdot \gcd(a,b)$ is a multiple of $a+b$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le 10^4$). The description of the test cases follows.

The first line of each test case contains two integers $n$, $m$ ($1\le n,m\le 2 \cdot 10^6$).

It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^6$.

Output

For each test case, print a single integer: the number of valid pairs.

Example

input

6
1 1
2 3
3 5
10 8
100 1233
1000000 1145141

output

0
1
1
6
423
5933961

Note

In the first test case, no pair satisfies the conditions.

In the fourth test case, $(2,2),(3,6),(4,4),(6,3),(6,6),(8,8)$ satisfy the conditions.

 

解题思路

  令 $d = \gcd(a, b)$,则有 $a = p \cdot d$,$b = q \cdot d$,且 $\gcd(p,q) = 1$。对于 $(a+b) \mid b \cdot d$,等价于 $(p+q) \mid q \cdot d$。又因为 $\gcd(q, p + q) = \gcd(q, p) = 1$,因此有 $(p+q) \mid d$。

  因此 $(a+b) \mid b \cdot \gcd(a, b)$ 等价于 $(p+q) \mid q \cdot d$。

  此时考虑能否枚举数对 $(p,q)$。事实上 $p < d = \frac{n}{p} \Longrightarrow p^2 < n$,同理 $q^2 < m$,因此这样的数对最多有 $O(\sqrt{n} \sqrt{m})$。所以我们直接暴力枚举找到满足 $\gcd(p,q) = 1$ 的 $p$ 和 $q$,由于此时 $d$ 最大能取到 $\min\left\{ \left\lfloor\frac{n}{p}\right\rfloor, \left\lfloor\frac{m}{q}\right\rfloor \right\}$,因此满足 $(p+q) \mid q \cdot d$ 对应的 $(a,b)$ 的数量就是 $\left\lfloor\frac{\min\left\{ \left\lfloor\frac{n}{p}\right\rfloor, \left\lfloor\frac{m}{q}\right\rfloor \right\}}{p+q}\right\rfloor$。

  AC 代码如下,时间复杂度为 $O\left(\sqrt{n} \sqrt{m} \, \log{\max\{n,m\}}\right)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

void solve() {
    int n, m;
    scanf("%d %d", &n, &m);
    LL ret = 0;
    for (int i = 1; i * i < n; i++) {
        for (int j = 1; j * j < m; j++) {
            if (__gcd(i, j) == 1) ret += min(n / i, m / j) / (i + j);
        }
    }
    printf("%lld\n", ret);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 942 (Div. 1, Div. 2) Editorial:https://codeforces.com/blog/entry/129027

标签:le,gcd,cdot,Hard,Version,right,test,D2,left
From: https://www.cnblogs.com/onlyblues/p/18170442

相关文章

  • You have an error in your SQL syntax; check the manual that corresponds to your
    在进行增加User对象时采用了PreparedStatement对象,方法executeQuery()和executeUpdate()都是无参方法,所以不要写成prepareStatement.executeQuery(sql)或prepareStatement.executeUpdate(sql)publicintadd(Useruser){Connectionconnection=JDBCTools.getConnecti......
  • 简单解决version 'GLIBC_2.34' not found,version 'GLIBC_2.25' not found
    简单解决version'GLIBC_2.34'notfound,version'GLIBC_2.25'notfound无需手动下载安装包编译前言很多博客都是要手动下载安装包进行编译升级,但这样很容易导致系统崩溃,本博文提供一个简单的方法,参考自博客1,博客2.检查版本strings/usr/lib64/libc.so.6|grepGLIBC_或者......
  • 报错“Please indicate a valid Swagger or OpenAPI version field”
    报错“PleaseindicateavalidSwaggerorOpenAPIversionfield”报错信息PleaseindicateavalidSwaggerorOpenAPIversionfield.Supportedversionfieldsareswagger:"2.0"andthosethatmatchopenapi:3.0.n(forexample,openapi:3.0.0). 原因分析根......
  • CF1967B2 Reverse Card (Hard Version) 题解
    题意:求有多少对\((a,b)\)满足\(b\times\gcd(a,b)\equiv0\pmod{a+b},1\lea\len,1\leb\lem\)。首先我们设\(\gcd(a,b)=G,a=i\timesG,b=j\timesG\),显然有\(\gcd(i,j)=1\)。那么可以把原条件转化为\(j\timesG\)是\((i+j)\)的倍数。因为\(\gcd(i+......
  • Reverse Card (Hard Version)
    事情是这样的,我验了这一场CF。显然我玩原神玩多了有一个很奇怪的、不能过的算法,哦,当然,在我本机可以过。为了展现自己的智慧糖,我写一下。出题人是先发给我了一个限制都是\(n\)的,因此只有这个。\(n,m\)改改就是了。要求\(1\lea\len,1\leb\len\)满足\(a+b\midb\times......
  • ES Validation Failed: 1: this action would add [1] shards, but this cluster c
    [2024-05-01T08:56:52,606][ERROR][o.e.x.i.IndexLifecycleRunner][tools]policy[ilm-history-ilm-policy]forindex[.ds-ilm-history-5-2024.03.28-000001]failedonstep[{"phase":"hot","action":"rollover","name&qu......
  • Intel Pentium III CPU(Coppermine, Tualatin) L2 Cache Latency, Hardware Prefetch
    这几天,偶然的机会想到了困扰自己和其他网友多年的IntelPentiumIII系列处理器缓存延迟(L2CacheLatency),以及图拉丁核心版本是否支持硬件预取(HardwarePrefetch)问题。手头的支持图拉丁核心处理器的i815主板还在正常服役中,铜矿和图拉丁核心处理器也都有,所以就专门做了这一期调查,感......
  • Installation requirements for SAP Kernels on Windows (C++ runtime environment, V
      Symptom在Windows执行StartSAP,报错信息找不到指定的模块:"Theprogramcan'tstartbecausemsvcr100.dllismissingfromyourcomputer.""无法启动此程序,因为计算机中丢失了msvcr100.dll。尝试重新安装该程序以解决此问题。" OtherTermsC,C++,runtime,VCred......
  • AssemblyResolve巧解未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0的问题
    问题:未能加载文件或程序集“Newtonsoft.Json,Version=6.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个...问题分析:原因是因为引用的Microsoft.AspNet.SignalR.Client库的依赖项是6.0版本的Newtonsoft.Json,而且是动态加载进去的(用Assembly.LoadFrom),......
  • CH58x芯片Hardfault问题排查
    前言:针对RISC-V芯片进入HardFault_Handler函数的问题排查提供讲解。一、HardFault函数添加PC指针打印在公共文件的sys.c函数中找到函数并修改如下:__INTERRUPT__HIGH_CODE__attribute__((weak))voidHardFault_Handler(void){uint32_tv_mepc,v_mcause,v_mtval;p......