• 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. }

  • 相关阅读:
    1-(4-氨基苯)-1,2,2-三苯乙烯|AIE聚集诱导发光|1-(4-Aminophenyl)-1,2,2-triphenylethene
    MySQL 生僻概念汇总
    25.中间件
    机器人任务挖掘与智能超级自动化技术解析
    【Selenium】WebDriverPool让动态爬虫变得更简单高效稳定
    JVM第七讲:JVM 基础 - Java 内存模型详解
    数据挖掘:遗传算法GA Genetic Algorithms
    Java版本spring cloud + spring boot企业电子招投标系统源代码
    MySQL数据库索引
    PyTorch JIT和TorchScript,一个API提升推理性能50%
  • 原文地址:https://blog.csdn.net/m0_63054077/article/details/126412152