Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)
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", ×);
while(times --){
ll x;
scanf("%lld", &x);
get_fact(x);
printf("Case %d: %lld\n", ++ _case, work());
}
return 0;
}
Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)的更多相关文章
- Pairs Forming LCM LightOJ - 1236 素因子分解
Find the result of the following code: long long pairsFormLCM( int n ) { long long res = 0; fo ...
- 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 ...
- Pairs Forming LCM LightOJ - 1236 (算术基本定理)
题意: 就是求1-n中有多少对i 和 j 的最小公倍数为n (i <= j) 解析: 而这题,我们假设( a , b ) = n ,那么: n=pk11pk22⋯pkss, a=pd11pd2 ...
- Aladdin and the Flying Carpet (LightOJ - 1341)【简单数论】【算术基本定理】【分解质因数】
Aladdin and the Flying Carpet (LightOJ - 1341)[简单数论][算术基本定理][分解质因数](未完成) 标签:入门讲座题解 数论 题目描述 It's said ...
- Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】
Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
- LightOJ 1236 - Pairs Forming LCM(素因子分解)
B - Pairs Forming LCM Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 1236 - Pairs Forming LCM
1236 - Pairs Forming LCM Find the result of the following code: long long pairsFormLCM( int n ) { ...
- Pairs Forming LCM(素因子分解)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B 全题在文末. 题意:在a,b中(a,b<=n) ...
随机推荐
- Redis的优缺点小结
Redis(Remote Dictionary Server 远程数据服务),一个 Key-value(键值对)存储系统,典型的 NoSQL 数据库服务器. 优点: 1.支持丰富的数据类型,如:Str ...
- JS系列:js节点
节点(node) 在html文档中出现的所有东西都是节点 元素节点(HTML标签) 文本节点(文字内容) 注释节点(注释内容) 文档节点(document) … 每一种类型的节点都会有一些属性区分自己 ...
- 【Maven】聚合
[Maven]聚合 转载: 使用聚合一次能为多个 maven 项目执行命令,而不用到每一个项目下去执行命令. 聚合 pom 的特殊之处 1.packaging 配置 pom <packaging ...
- 【Java Web开发学习】Spring MVC整合WebSocket通信
Spring MVC整合WebSocket通信 目录 ========================================================================= ...
- MYSQL-JDBC批量新增-更新-删除
目录 1 概述 2 开启MYSQL服务端日志 3 深入MYSQL/JDBC批量插入 3.1 从一个例子出发 3.2 JDBC的批量插入操作 3.3 两个常被忽略的问题 3.5 误区 4 MYSQL/J ...
- CCF-CSP题解 201709-4 通信网络
dfs #include <bits/stdc++.h> const int maxn = 1000; const int maxm = 10000; using namespace st ...
- 线程中put(None)和主函数中put(None)的区别和用法
''' 初试生产者消费者模型代码 分析: 对象含有生产者.队列.消费者 Queue队列模块,不适合传大文件,通常传一些消息. ''' '''多生产者进程和多消费者进程''' #导入模块 from mu ...
- Linux下shell通用脚本启动jar(微服务)
Linux下shell通用脚本启动jar(微服务) vim app_jar.sh #!/bin/bash #source /etc/profile # Auth:Liucx # Please chan ...
- 聊聊 print 的前世今生
本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/NuzfuH_zCZzcrmSFR04NHw (一) 上周,我翻译了一篇 ...
- JavaScript动画实例:旋转的圆球
1.绕椭圆轨道旋转的圆球 在Canvas画布中绘制一个椭圆,然后在椭圆上绘制一个用绿色填充的实心圆.之后每隔0.1秒刷新,重新绘制椭圆和实心圆,重新绘制时,实心圆的圆心坐标发生变化,但圆心坐标仍然位于 ...