首页 技术 正文
技术 2022年11月16日
0 收藏 979 点赞 4,012 浏览 1770 个字

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

152346810121518202124252729

Sample Output

Case 1: 2Case 2: 2Case 3: 3Case 4: 5Case 5: 4Case 6: 5Case 7: 8Case 8: 5Case 9: 8Case 10: 8Case 11: 5Case 12: 11Case 13: 3Case 14: 4Case 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
SharedRemoteRunId
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;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898