Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)

标签: 入门讲座题解 数论


题目描述

Find the result of the following code:

  1. long long pairsFormLCM( int n ) {
  2. long long res = 0;
  3. for( int i = 1; i <= n; i++ )
  4. for( int j = i; j <= n; j++ )
  5. if( lcm(i, j) == n ) res++; // lcm means least common multiple
  6. return res;
  7. }

A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).

Input

  1. Input starts with an integer T (≤ 200), denoting the number of test cases.
  2. Each case starts with a line containing an integer n (1 n 1014).

Output

  1. For each case, print the case number and the value returned by the function 'pairsFormLCM(n)'.

Sample Input

  1. 15
  2. 2
  3. 3
  4. 4
  5. 6
  6. 8
  7. 10
  8. 12
  9. 15
  10. 18
  11. 20
  12. 21
  13. 24
  14. 25
  15. 27
  16. 29

Sample Output

  1. Case 1: 2
  2. Case 2: 2
  3. Case 3: 3
  4. Case 4: 5
  5. Case 5: 4
  6. Case 6: 5
  7. Case 7: 8
  8. Case 8: 5
  9. Case 9: 8
  10. Case 10: 8
  11. Case 11: 5
  12. Case 12: 11
  13. Case 13: 3
  14. Case 14: 4
  15. Case 15: 2

题意

给定\(n\)。

求\(\sum_{i = 1}^{n} \sum_{j = i}^{n} [lcm(i, j) = n]\)的值。


解析


通过代码

  1. /*
  2. Problem
  3. LightOJ - 1236
  4. Status
  5. Accepted
  6. Time
  7. 410ms
  8. Memory
  9. 19664kB
  10. Length
  11. 1299
  12. Lang
  13. C++
  14. Submitted
  15. 2019-11-25 15:30:08
  16. Shared
  17. RemoteRunId
  18. 1640611
  19. */
  20. #include <bits/stdc++.h>
  21. using namespace std;
  22. typedef long long ll;
  23. const int MAXN = 1e7 + 50;
  24. bool vis[MAXN];
  25. int prime[MAXN / 10], p[MAXN / 10], m = 0, cnt;
  26. void fill_0()
  27. {
  28. for(int i = 1; i <= cnt; i ++)
  29. p[i] = 0;
  30. return;
  31. }
  32. void get_prime() //线性筛,筛出1e7以内全部质数.
  33. {
  34. vis[1] = 1;
  35. for(int i = 2; i <= int(1e7 + 5); i ++){
  36. if(!vis[i])
  37. prime[++ m] = i;
  38. for(int j = 1; j <= m && i * prime[j] <= int(1e7 + 5); j ++){
  39. vis[i * prime[j]] = 1;
  40. if(i % prime[j] == 0)
  41. break;
  42. }
  43. }
  44. return;
  45. }
  46. void get_fact(ll x)
  47. {
  48. fill_0(); //将p数组归零.
  49. cnt = 0;
  50. for(int i = 1; i <= m && 1ll * prime[i] * prime[i] <= x; i ++){ //以下求得一个数的质因数分解每个质数的幂次.
  51. if(x % prime[i] == 0){
  52. cnt ++;
  53. while(x % prime[i] == 0){
  54. p[cnt] ++;
  55. x /= prime[i];
  56. }
  57. }
  58. }
  59. if(x != 1)
  60. p[++ cnt] = 1;
  61. return;
  62. }
  63. ll work()
  64. {
  65. ll res = 1;
  66. for(int i = 1; i <= cnt; i ++)
  67. res *= 1ll * (2 * p[i] + 1);
  68. return (res + 1) >> 1;
  69. }
  70. int main()
  71. {
  72. get_prime();
  73. int times, _case = 0;
  74. scanf("%d", &times);
  75. while(times --){
  76. ll x;
  77. scanf("%lld", &x);
  78. get_fact(x);
  79. printf("Case %d: %lld\n", ++ _case, work());
  80. }
  81. return 0;
  82. }

Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)的更多相关文章

  1. Pairs Forming LCM LightOJ - 1236 素因子分解

    Find the result of the following code: long long pairsFormLCM( int n ) {    long long res = 0;    fo ...

  2. G - Pairs Forming LCM LightOJ - 1236 (质因子分解)

    题解:这道题要从n的角度来考虑i和j. n可以表示为n=a1^p1*a2^p2*a3^p3.......n=lcm(i,j),那么质因子a1^p1,a1可以在i或者j中,并且p1=max(a1i,a1 ...

  3. Pairs Forming LCM LightOJ - 1236 (算术基本定理)

    题意: 就是求1-n中有多少对i 和 j 的最小公倍数为n  (i <= j) 解析: 而这题,我们假设( a , b ) = n ,那么: n=pk11pk22⋯pkss, a=pd11pd2 ...

  4. Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】

    Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...

  5. Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】

    Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...

  6. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  7. LightOJ 1236 - Pairs Forming LCM(素因子分解)

    B - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  8. 1236 - Pairs Forming LCM

    1236 - Pairs Forming LCM   Find the result of the following code: long long pairsFormLCM( int n ) {  ...

  9. Pairs Forming LCM(素因子分解)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B    全题在文末. 题意:在a,b中(a,b<=n) ...

随机推荐

  1. 【React】360- 完全理解 redux(从零实现一个 redux)

    点击上方"前端自习课"关注,学习起来~ 前言 记得开始接触 react 技术栈的时候,最难理解的地方就是 redux.全是新名词:reducer.store.dispatch.mi ...

  2. 深入学习CSS3-flexbox布局

    学习博客:https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 学习博客:http://caibaojian.com/demo/flexbox ...

  3. 四步实现在一台电脑上使用多个github账号

    四步实现在一台电脑上同时使用多个GitHub账号 今天和大家聊一下如何在一台电脑上同时使用多个GitHub账号,通过以下四个步骤就可以实现,其中第二个步骤为了便于叙述分成了几个小步骤. 1. 取消全局 ...

  4. vue解惑之slot(插槽)

    一.插槽是个什么玩意,能吃吗 在vue中[插槽],从字面意思来看,插槽意味着[内容的增加],回到vue的使用场景,插槽就是[父组件调用子组件时,额外增加的内容]. 插槽显不显示.显示的内容是由父组件来 ...

  5. 找不到UseInMemoryDatabase方法

    创建WebApi项目时,在ConfigureServices中注册数据库上下文时,提示找不到UseInMemoryDatabase方法. 打开“工具-Nuget包管理器-程序包管理器控制台”,输入“I ...

  6. 域控权限提升PTH攻击

    0x01 漏洞利用条件 1.被pth攻击的计算机未打补丁(KB2871997)2.拿到一台域成员主机并且拿到管理员组的域用户的NTML 3.对方主机存在相同账号并且是管理员组成员 0x02 本地用户N ...

  7. 「SAP技术」SAP VL02N 执行批次拆分报错,说不允许批次拆分?

    1,如下新建的DN 80017843,storage location 字段值为空.VL02N 试图去做批次拆分失败,系统报错说,Batch split is not permitted for ma ...

  8. Android 开发凉了吗!

    昨天我拿了本<安卓开发大全>的书,把它放进了冰箱,你猜怎么样? 它凉了. 记得2013年的时候,安卓崛起,一夜之间遍地谈论安卓这个奇怪的机器人. 安卓受宠的原因,主要围绕着: 1 应用商城 ...

  9. java 获取当前年份 月份,当月第一天和最后一天

    获取当前年份 月份,当月第一天和最后一天,工作中会经常用到,下面是代码: package basic.day01; import java.text.SimpleDateFormat; import ...

  10. linux部署.net Core项目

    首篇笔记,多多关照.方便回忆和给新手指导,大神绕道 首先在Linux系统部署.net Core项目首先准备一个Linux系统的服务器,百度云,阿里云都行. 1.net core 部署在Linux系统上 ...