B - Pairs Forming LCM

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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 求满足lcm(a, b) == n, a <= b 的 (a,b) 的个数

容易知道 n 是a, b的所有素因子取在a, b中较大指数的积

先将n分解为素数指数积的形式 n = π(pi^ei) 那么对于每个素因子pi pi在a,b中的指数ai, bi 至少有一个等于pi, 另一个小于等于pi

先不考虑a, b的大小 对于每个素因子pi

1. 在a中的指数 ai == ei 那么 pi 在 b 中的指数可取 [0, ei] 中的所有数 有 ei + 1 种情况

2. 在a中的指数 ai < ei 即 ai 在 [0, ei) 中 那么 pi 在 b 中的指数只能取 ei 有 ei 种情况

那么对于每个素因子都有 2*ei + 1种情况 也就是满足条件的 (a, b) 有π(2*ei + 1)个 考虑大小时除了 (n, n) 所有的情况都出现了两次 那么满足a<=b的有(π(2*ei + 1)) / 2 + 1

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e7+;//这里不能用typedef 1e7
int prime[maxn/]; //这里一定要除以10 否则会超内存 一共664579个素数
int cnt=;
bool isprime[maxn];
void getprime() //素筛打表
{
for(int i=;i<=maxn;i++)
{
if(!isprime[i])
{
prime[cnt++]=i;
for(int j=i+i;j<=maxn;j+=i)
isprime[j]=;
}
}
}
int main()
{
getprime(); //先get素数表
//cout<<cnt<<endl;
int t,cas=;
cin>>t;
while(t--)
{
ll n,sum,ans=;
cin>>n;
for(int i=;i<cnt;i++)
{
sum=;
if(prime[i]*prime[i]>n) //记得break
break;
if(n%prime[i]==)
{
while(n%prime[i]==)
{
n/=prime[i];
sum++;
}
ans=ans*(sum*+);
}
}
if(n>) ans*=;
ans=ans/+;
printf("Case %d: %lld\n",cas++,ans);
}
return ;
}

LightOJ 1236 - Pairs Forming LCM(素因子分解)的更多相关文章

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

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

  2. LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i,  j)满足 LCM(i, j) = n, ...

  3. LightOJ - 1236 - Pairs Forming LCM(唯一分解定理)

    链接: https://vjudge.net/problem/LightOJ-1236 题意: Find the result of the following code: long long pai ...

  4. LightOJ 1236 Pairs Forming LCM【整数分解】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1236 题意: 找与n公倍数为n的个数. 分析: ...

  5. LightOJ 1236 Pairs Forming LCM 合数分解

    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...

  6. 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) == ...

  7. 1236 - Pairs Forming LCM

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

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

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

  9. Light oj 1236 - Pairs Forming LCM (约数的状压思想)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意很好懂,就是让你求lcm(i , j)的i与j的对数. 可以先预处理1e7以 ...

随机推荐

  1. jquery 获取datagrid行数

    var data = $('#dg').datagrid('getData'); alert('总数据量:' + data.total)//注意你的数据源一定要定义了total,要不会为undefin ...

  2. 大数据之tachyon(未完版)

    1.内存文件存储系统 Tachyon是一个开源分布式存储系统,拥有高性能.高容错等优点.并具有类Java的文件API.插件式的底层文件系统.兼容Hadoop MapReduce和 Apache Spa ...

  3. 【PHP面向对象(OOP)编程入门教程】1.什么是面向对象?

    面向对象编程(Object Oriented Programming, OOP, 面向对象程序设计)是一种计算机编程架构,OOP的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成 ...

  4. Windows下几个常用的和进程有关的命令

    在windows下,进入cmd,有几个常用的和进程有关的命令: netstat -ano:查看所有进程 netstat -ano|findstr  “端口号”:查看端口号对应的进程PID taskli ...

  5. 02快速学习ExtJs之---第一个HelloWord!

    这篇主要讲部署下ExtJS开发环境,以及搭建项目.我们使用ExtJs官方提供的Sencha Cmd来搭建 1.搭建项目 1.下载官方的Sencha Cmd工具,安装. 2..Window用户进入到命令 ...

  6. FineUI第十八天---表格之事件的处理

    表格之事件的处理: 1.事件参数: GridPageEventArgs:表格分页事件参数,对应onPageIndexChange事件. NewPageIndex:新页面的索引 GridSortEven ...

  7. Java 的printf(转)

    出处:http://blog.csdn.net/swandragon/article/details/4653600 public class TestPrintf{public static voi ...

  8. iOS开发——UI进阶篇(十三)UITabBarController简单使用,qq主流框架

    一.UITabBarController简单使用 // 程序加载完毕 - (BOOL)application:(UIApplication *)application didFinishLaunchi ...

  9. 开源多线程性能测试工具-sysbench

    导读 sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试.数据库目前支持MySQL/Oracle/PostgreSQL.本文主要演示Mysql测试 ...

  10. Sqli-LABS通关笔录-2

    在这个关卡学习到了 1.程序的错误不是学校收费乱来的,单引号的错误和减号的错误要明白 单引号报错. 用第一关的PAYLOAD尝试了下. 来看看源码: 那么我们构造的sql也就变成了 $sql=&quo ...