1236 - Pairs Forming LCM
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)'
分析:题意1到n中存在多少对(a,b)满足lcm(a, b)==n。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
int prim[maxn/10] ;
int k;
int vis[maxn];
void init() //线性筛
{
k = 0;
for(int i = 2; i < maxn; ++i)
{
if(!vis[i]) prim[k++] = i;
for(int j = 0; j < k && prim[j] * i < maxn; ++j)
{
vis[prim[j] * i] = 1;
if(i % prim[j] == 0) break;
}
}
}
int main(void)
{
int T, cas;
ll n;
scanf("%d", &T);
cas = 0;
init();
while(T--)
{
cas++;
scanf("%lld", &n);
ll ans = 1;
for(int i = 0; i < k; i++)
{
ll x = 0;
if((ll)(prim[i]) * prim[i] > n)
break;
while(n % prim[i] == 0)
{
x++;
n /= prim[i];
}
if(x)
ans *= 2 * x + 1;
}
if(n > 1)///n>1表示最后还剩一个素因子,比如6,20,并且这个素因子的平方大于n,再循环中没有处理。剩余最后一个因子按分析中的方法它对应三种取法,所以最后乘在ans上。
ans *= 3;
printf("Case %d: %lld\n", cas, ans / 2 + 1);
}
return 0;
}
1236 - Pairs Forming LCM的更多相关文章
- 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 ...
- LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
- Light oj 1236 - Pairs Forming LCM (约数的状压思想)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意很好懂,就是让你求lcm(i , j)的i与j的对数. 可以先预处理1e7以 ...
- LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)
链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...
- 1236 - Pairs Forming LCM -- LightOj1236 (LCM)
http://lightoj.com/volume_showproblem.php?problem=1236 题目大意: 给你一个数n,让你求1到n之间的数(a,b && a<= ...
- LightOJ 1236 Pairs Forming LCM 合数分解
题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...
- LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)
题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...
- LightOJ 1236 Pairs Forming LCM【整数分解】
题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1236 题意: 找与n公倍数为n的个数. 分析: ...
随机推荐
- 简单介绍HTTP的请求(get请求和post请求)以及对应的响应的内容
链接解析: https://oa.hbgf.net.cn/login.jsp;jsessionid=47084322738F8DB18D60752944DFD1AA http或者https表示使用的是 ...
- python 打印乘法表
for i in range(1, 10): for j in range(1, i+1): print('%s * %s = %s' % (i, j, i*j), end=' ') print('' ...
- options请求(复杂请求)
1.请求发送: HEAD. GET. POST2.请求头信息: Accept Accept-Language Content-Language Last-Event-ID ...
- Windows Terminal入门
目录 0.引言 1.简易安装 2.初识WT 3.初识Settings 3.1全局配置 3.2每一个终端配置 3.3配色方案 3.4键位绑定 4.连接云服务器 5.连接WSL 6.玩转Emoji 0.引 ...
- Linux系统搭建私有CA证书服务器
一.CA简介 CA是什么?CA是Certificate Authority的简写,从字面意思翻译过来是凭证管理中心,认证授权.它有点类似我们生活中的身份证颁发机构,这里的CA就相当于生活中颁发身份证的 ...
- 对接口运用扩展方法 Applying Extension Methods to an Interface 精通ASP-NET-MVC-5-弗瑞曼 Listing 4-15
- Rainbow Plan团队项目第一次作业——项目计划
团队项目--Rainbow Plan英语学习App 1.团队简介 1.1团队名称:Rainbow Plan (彩虹计划) 1.2团队成员: 队员学号 队员姓名 201731024235 何继武(组长) ...
- BAT脚本每隔30秒创建一个512兆文件(测试磁盘监控用)
@echo offsetlocal ENABLEDELAYEDEXPANSIONset /a ii=0 for /l %%i in (1,1,100) do (set /a ii+=1ping -n ...
- VScode前端插件推荐
工欲善其事,必先利其器,安利一波前端插件. Chinese (Simplified) Language Pack for Visual Studio CodeVScode汉化插件 Beautify代码 ...
- 自定义BeanDefinitionRegistryPostProcessor
自定义BeanDefinitionRegistryPostProcessor 概述 BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProce ...