• LCM Sum (hard version)(树状数组,筛因子)


    目录

    思路:

    代码:


     

    LCM Sum (hard version)

    This version of the problem differs from the previous one only in the constraint on tt. You can make hacks only if both versions of the problem are solved.

    You are given two positive integers ll and rr.

    Count the number of distinct triplets of integers (i,j,k)(i,j,k) such that l≤irl≤i

    Here lcm(i,j,k)lcm⁡(i,j,k) denotes the least common multiple (LCM) of integers ii, jj, and kk.

    Input

    Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051≤t≤105). Description of the test cases follows.

    The only line for each test case contains two integers ll and rr (1≤l≤r≤2⋅1051≤l≤r≤2⋅105, l+2≤rl+2≤r).

    Output

    For each test case print one integer — the number of suitable triplets.

    Example

    input

    Copy

     
    

    5

    1 4

    3 5

    8 86

    68 86

    6 86868

    output

    Copy

    3
    1
    78975
    969
    109229059713337
    

    Note

    In the first test case, there are 33 suitable triplets:

    • (1,2,3)(1,2,3),
    • (1,3,4)(1,3,4),
    • (2,3,4)(2,3,4).

    In the second test case, there is 11 suitable triplet:

    • (3,4,5)(3,4,5).

    思路:

    正难则反

    1,讨论总的情况:(r-l+1)*(r-l-1)/6

    2,讨论lcm的情况:lcm(i,j,k)lcm<3*k,=>lcm为k或者2*k

    3,讨论i,j的情况:

    当lcm为2*k时:

    1)k/22t=3=>j=2/3*k

    2)2*kkk/32i=k/2||k*2/5

    当lcm为k时:

    i

    4,树状数组记载每个数作为i的可行情况数

    代码:

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. #define int long long
    4. #define vec vector<int>
    5. #define pii pair<int,int>
    6. #define pi 3.1415926
    7. #define rep(i,l,r) for(int i=l;i<=r;++i)
    8. #pragma GCC optimize(2)//
    9. #pragma GCC optimize(3,"Ofast","inline")//
    10. const int maxj=2e5+10,mod=1e9+7,inf=0x3f3f3f3f;
    11. struct bit{
    12. int sum[maxj];
    13. int lowbit(int x){return x&-x;}
    14. void add(int x,int c){while(x<=maxj)sum[x]+=c,x+=lowbit(x);}
    15. int getsum(int x){int res=0;while(x)res+=sum[x],x-=lowbit(x);return res;}
    16. }tree;
    17. vector<pii>a[maxj];
    18. int ans[maxj];
    19. vec fac[maxj];//存因子
    20. void solve(){
    21. int n;cin>>n;
    22. for(int i=1;i<maxj;++i){//存因子
    23. for(int j=i*2;j<maxj;j+=i){
    24. fac[j].emplace_back(i);
    25. }
    26. }
    27. for(int i=1;i<=n;++i){//
    28. int l,r;cin>>l>>r;
    29. a[r].push_back({l,i});//存数组,l,r
    30. int x=r-l+1;
    31. ans[i]=x*(x-1)*(x-2)/6;//正难则反
    32. }
    33. for(int r=1;r<maxj;++r){//既是k,又是r,暴力得出答案
    34. //公因子2*k,找r以内的因子
    35. for(int j=0;j<fac[r].size();++j){
    36. int x=fac[r][j];
    37. int cnt=0;//计数器
    38. for(int k=j+1;k<fac[r].size();++k){
    39. int y=fac[r][k];
    40. if(r%x==0&&r%y==0){
    41. cnt+=1;
    42. }
    43. }
    44. tree.add(x,cnt);
    45. }
    46. //j为2/3*k
    47. //i是k/2
    48. if(r%6==0){
    49. tree.add(r/2,1);
    50. }
    51. //i是2/5*k
    52. if(r%15==0){
    53. tree.add(r*2/5,1);
    54. }
    55. for(auto i:a[r]){//计算(l,r-2),
    56. ans[i.second]-=(tree.getsum(r-2)-tree.getsum(i.first-1));
    57. }
    58. }
    59. for(int i=1;i<=n;++i){
    60. cout<<ans[i]<<'\n';
    61. }
    62. }
    63. signed main(){
    64. ios::sync_with_stdio(0);
    65. cin.tie(0);
    66. cout.tie(0);//与快读冲突
    67. int t;
    68. t=1;
    69. // cin>>t;
    70. while(t--)solve();
    71. return 0;
    72. }

  • 相关阅读:
    纯粹的四位奇数
    JVM学习八
    聚观早报 | 网传恒大员工“停工留职”;腾讯WiFi管家停止服务
    C语言学习:3、数据输入
    混沌系统在图像加密中的应用(荷控忆阻器的混沌电路)
    史上最简SLAM零基础解读(8) - g2o(图优化)→示例代码讲解
    socket与rpc的区别
    Vue笔记基础
    [附源码]java毕业设计基于ssm的电子网上商城
    vue3—elementPlus如何单独修改页面中的某个下拉框样式
  • 原文地址:https://blog.csdn.net/m0_63054077/article/details/126412152