• Codeforces Round #818 (Div. 2)


    E. Madoka and The Best University

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    Madoka wants to enter to "Novosibirsk State University", but in the entrance exam she came across a very difficult task:

    Given an integer nn, it is required to calculate ∑lcm(c,gcd(a,b))∑lcm⁡(c,gcd(a,b)), for all triples of positive integers(a,b,c), where a+b+c=n.

    In this problem gcd(x,y) denotes the greatest common divisor of x and y, and lcm(x,y) denotes the least common multiple of x and y.

    Solve this problem for Madoka and help her to enter to the best university!

    Input

    The first and the only line contains a single integer nn (3≤n≤10^5).

    Output

    Print exactly one interger — ∑lcm⁡(c,gcd(a,b)). Since the answer can be very large, then output it modulo 109+7.

    Examples

    input

    3
    

    output

    1
    

    input

    5
    

    output

    11
    

    input

    69228
    

    output

    778304278
    

    Note

    In the first example, there is only one suitable triple (1,1,1) So the answer is lcm(1,gcd(1,1))=lcm(1,1)=1.

    In the second example, lcm(1,gcd(3,1))+lcm(1,gcd(2,2))+lcm(1,gcd(1,3))+lcm(2,gcd(2,1))+lcm(2,gcd(1,2))+lcm(3,gcd(1,1))=lcm(1,1)+lcm(1,2)+lcm(1,1)+lcm(2,1)+lcm(2,1)+lcm(3,1)=1+2+1+2+2+3=11

     

     

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. typedef long long ll;
    4. const int mod=1e9+7;
    5. const int N = 1e5+5;
    6. int phi[N], p[N], cnt;
    7. vector<int>G[N];
    8. int n;
    9. bool vis[N];//1即为合数,为0则为质数
    10. void getphi(int n)
    11. {
    12. phi[1] = 1;
    13. for (int i=2; i<=n; i++)
    14. {
    15. if (!vis[i]) p[++cnt]=i,phi[i]=i-1;
    16. for (int j=1; j<=cnt&&i*p[j]<=n; j++)
    17. {
    18. vis[i*p[j]]=1;
    19. if (i%p[j]==0)
    20. {
    21. phi[i*p[j]]=phi[i]*p[j];
    22. break;
    23. }
    24. else phi[i*p[j]]=phi[i] * phi[p[j]];
    25. }
    26. }
    27. phi[1]=0;
    28. }
    29. ll lcm(ll x,ll y)
    30. {
    31. return x*y/__gcd(x,y);
    32. }
    33. void getdiv()
    34. {
    35. for(int i=1;i<N;i++)
    36. {
    37. for(int j=i;j<N;j+=i)
    38. {
    39. G[j].push_back(i);
    40. }
    41. }
    42. }
    43. void solve()
    44. {
    45. int n;
    46. cin>>n;
    47. ll ans = 0;
    48. getdiv();
    49. for(int c=1; c<n; c++)
    50. {
    51. for(auto x:G[n-c])
    52. {
    53. ans = (ans+(ll)lcm(c, x)*(ll)phi[(n-c)/x]%mod)%mod;
    54. }
    55. }
    56. cout<<ans;
    57. }
    58. int main()
    59. {
    60. ios::sync_with_stdio(false);
    61. cin.tie(0);
    62. cout.tie(0);
    63. getphi(1e5);
    64. solve();
    65. return 0;
    66. }

     n*logn*logn

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. typedef long long ll;
    4. const int mod=1e9+7;
    5. const int N = 1e5+5;
    6. int phi[N], p[N], cnt;
    7. int n;
    8. bool vis[N];//1即为合数,为0则为质数
    9. void getphi(int n)
    10. {
    11. phi[1] = 1;
    12. for (int i=2; i<=n; i++)
    13. {
    14. if (!vis[i]) p[++cnt]=i,phi[i]=i-1;
    15. for (int j=1; j<=cnt&&i*p[j]<=n; j++)
    16. {
    17. vis[i*p[j]]=1;
    18. if (i%p[j]==0)
    19. {
    20. phi[i*p[j]]=phi[i]*p[j];
    21. break;
    22. }
    23. else phi[i*p[j]]=phi[i] * phi[p[j]];
    24. }
    25. }
    26. phi[1]=0;
    27. }
    28. ll lcm(ll x,ll y)
    29. {
    30. return x*y/__gcd(x,y);
    31. }
    32. vector<int>factors(int n)
    33. {
    34. set<int>s;
    35. for(int i=1; i<=sqrt(n); i++)
    36. {
    37. if(n%i==0) s.insert(i);
    38. if(n%i==0&&i!=n/i) s.insert(n/i);
    39. }
    40. return vector<int>(s.begin(),s.end());
    41. }
    42. void solve()
    43. {
    44. int n;
    45. cin>>n;
    46. ll ans = 0;
    47. for(int c=1; c<n; c++)
    48. {
    49. for(int d:factors(n-c))
    50. {
    51. ans = (ans+(ll)lcm(c, d)*(ll)phi[(n-c)/d]%mod)%mod;
    52. }
    53. }
    54. cout<<ans;
    55. }
    56. int main()
    57. {
    58. ios::sync_with_stdio(false);
    59. cin.tie(0);
    60. cout.tie(0);
    61. getphi(1e5);
    62. solve();
    63. return 0;
    64. }

    n*sqrt(n)*logn

  • 相关阅读:
    Java程序部署位windows服务
    浏览器渲染原理以及重排与重绘
    FL Studio21功能测评水果FL音乐制作数字音频工作站
    费曼学习法 用输出倒逼输入
    Linux管道与重定向
    cola 架构简单记录
    算法与数据结构【30天】集训营——栈和队列的全套操作及易错知识点总结(07)
    一块GPU训练TB级推荐模型不是梦,OneEmbedding性能一骑绝尘
    SpringBoot集成webservice
    RHCE第八天上课笔记
  • 原文地址:https://blog.csdn.net/m0_61949623/article/details/126678387