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

标签: 入门讲座题解 数论


题目描述

Find the result of the following code:

long long pairsFormLCM( int n ) {
long long res = 0;
for( int i = 1; i <= n; i++ )
for( int j = i; j <= n; j++ )
if( lcm(i, j) == n ) res++; // lcm means least common multiple
return res;
}

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

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).

Output

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

Sample Input

15

2

3

4

6

8

10

12

15

18

20

21

24

25

27

29

Sample Output

Case 1: 2

Case 2: 2

Case 3: 3

Case 4: 5

Case 5: 4

Case 6: 5

Case 7: 8

Case 8: 5

Case 9: 8

Case 10: 8

Case 11: 5

Case 12: 11

Case 13: 3

Case 14: 4

Case 15: 2

题意

给定\(n\)。

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


解析


通过代码

/*
Problem
LightOJ - 1236
Status
Accepted
Time
410ms
Memory
19664kB
Length
1299
Lang
C++
Submitted
2019-11-25 15:30:08
Shared RemoteRunId
1640611
*/ #include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int MAXN = 1e7 + 50; bool vis[MAXN];
int prime[MAXN / 10], p[MAXN / 10], m = 0, cnt; void fill_0()
{
for(int i = 1; i <= cnt; i ++)
p[i] = 0;
return;
}
void get_prime() //线性筛,筛出1e7以内全部质数.
{
vis[1] = 1; for(int i = 2; i <= int(1e7 + 5); i ++){
if(!vis[i])
prime[++ m] = i; for(int j = 1; j <= m && i * prime[j] <= int(1e7 + 5); j ++){
vis[i * prime[j]] = 1; if(i % prime[j] == 0)
break;
}
} return;
}
void get_fact(ll x)
{
fill_0(); //将p数组归零.
cnt = 0;
for(int i = 1; i <= m && 1ll * prime[i] * prime[i] <= x; i ++){ //以下求得一个数的质因数分解每个质数的幂次.
if(x % prime[i] == 0){ cnt ++;
while(x % prime[i] == 0){ p[cnt] ++;
x /= prime[i]; } }
} if(x != 1)
p[++ cnt] = 1; return;
} ll work()
{
ll res = 1; for(int i = 1; i <= cnt; i ++)
res *= 1ll * (2 * p[i] + 1); return (res + 1) >> 1;
}
int main()
{
get_prime(); int times, _case = 0; scanf("%d", &times); while(times --){ ll x;
scanf("%lld", &x); get_fact(x); printf("Case %d: %lld\n", ++ _case, work());
} return 0;
}

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. Jmeter介绍以及脚本制作与调试

    目录 Jmeter介绍 Jmeter安装 Jmeter主要测试组件 Jmeter元件作用域与执行顺序 Jmeter运行原理 Jmeter脚本制作 Jmeter脚本调试 Jmeter介绍 Jmeter ...

  2. 【Eclipse】Spring Tool Suite插件

    Spring Tool Suite插件 最新下载地址:https://spring.io/tools3/sts/all 以前下载地址:https://spring.io/tools3/sts/lega ...

  3. 7个点说清楚spring cloud微服务架构

    前言 spring cloud作为当下主流的微服务框架,让我们实现微服务架构简单快捷,spring cloud中各个组件在微服务架构中扮演的角色如下图所示,黑线表示注释说明,蓝线由A指向B,表示B从A ...

  4. 《MySQL数据库》常用语法(一)

    MySQL从创建数据库到对表的增删改操作汇总. 1. 数据库操作: -- 查看所有的数据库 SHOW DATABASES ; -- 创建一个数据库,XXX表示数据库名称 CREATE DATABASE ...

  5. 阿里云linux镜像发布web项目时候 tomcat与域名映射

    tomcat 与 域名映射 一  准备工作: 阿里云服务器Linux镜像 及 域名备案和将域名解析至服务器,通过<暂不说,网上一大把或者参考阿里官方实例手册> 在Linux镜像中安装tom ...

  6. c语言入门到精通怎么能少了这7本书籍?

    C语言作为学编程最好的入门语言,对一个初进程序大门的小白来说是很有帮助的,学习编程能培养一个人的逻辑思维,而C语言则是公认的最符合人们对程序的认知的一款计算机语言,很多大学都选择了使用C语言作为大学生 ...

  7. JS Math对象、日期对象、函数、定时器

    Math对象 开平方:sqrt 绝对值:abs π:PI x的y次方:pow 四舍五入取整:round 向下取整:floor 向上取整:ceil 最大值:max 最小值: min 随机数:random ...

  8. composer入门 一些简单常用的命令介绍

    composer是什么 composer是PHP的插件依赖管理工具,我个人感觉和java的Maven.Gradle很类似. Windows OS下安装composer 参考: https://www. ...

  9. Android 基于ksoap2的webservice请求的学习

    [学习阶段] WebService网络请求? 其实我也是第一次遇到,之所以有这个需要是因为一些与 ERP 相关的业务,需要用到这样的一个请求方式. 开始学习WebService ①当然是百度搜索,这里 ...

  10. centos7安装python3.7.4

    yum install gcc make zlib  zlib-devel openssl openssl-devel libffi-devel bzip2-devel ncurses-devel g ...