A - (例题)整数分解

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Submit Status

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,让你寻找有多少i,j满足

lcm(i,j)=n&&1<=i<=j<=n

思路分析:首先n的范围十分大(1e14),暴力做肯定会超时,对于LCM,GCD,我们常考虑正整数唯一分解定理,

定理内容:对于任意一个大于1的数都可以唯一分解为若干个素数的乘积,即n=a1^b1*a2^b2*......an^bn;

我们先研究其中一个素因子a1,首先i和j唯一分解后肯定有a1^k(0~b1),同时又因为LCM(i,j)=n,则肯定有一个

数k=b1,可能的种数有(2*(b1+1)-1)(因为k1=b1&&k2=b1的情况多算了一次),由分步乘法技术原理可得

总共的可能性有t=2*b1+1)(2*b2+1)(2*b3+1)........,但是注意题目要求i<=j,i==j的情况只有可能有一种,那就是

i==j==n,由对称性,i<j的情况有(t-1)/2种,所以最后的答案就是(t+1)/2;

tip:正整数唯一分解需要进行两步 1.素数筛(到sqrt(n)即可) 2.枚举素数,进行唯一分解

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include<algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=1e7+;//
bool vis[maxn];
ll prime[maxn/];
int tot;
/*void getprime()//因为n的范围是1e14,打表只需要打到sqrt(n)即可,最多只可能有一个素因子大于sqrt(n),最后特判一下即可;
{
memset(vis,true,sizeof(vis));
tot=0;
for(ll i=2;i<maxn;i++)
{
if(vis[i])
{
prime[tot++]=i;
for(ll j=i*i;j<maxn;j+=i)
{
vis[j]=false;
}
}
}
}*/
void Eulerprime()
{
memset(vis,true,sizeof(vis));
int tot=;
for(int i=;i<maxn;i++)
{
if(vis[i]) prime[tot++]=i;
for(int j=;j<tot&&prime[j]*i<maxn;j++)
{
vis[i*prime[j]]=false;
if(i%prime[j]==) break;
}
}
}
int a[],b[];
int cnt=;
void sbreak(ll n)//正整数唯一分解
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
cnt=;
for(int i=;prime[i]*prime[i]<=n;i++)
{
if(n%prime[i]==)
{
a[cnt]=prime[i];
while(n%prime[i]==)
{
b[cnt]++;
n/=prime[i];
}
cnt++;
}
}
if(n!=)
{
a[cnt]=n;
b[cnt]=;
cnt++;//为了使两种情况分解后素因子下标都是0~cnt-1;
}
}
int kase;
int main()
{
int T;
ll n;
Eulerprime();
scanf("%d",&T);
kase=;
while(T--)
{
scanf("%lld",&n);
sbreak(n);
ll ans=;
for(ll i=;i<cnt;i++)
{
ans*=(*b[i]+);
}
ans=(ans+)/;
printf("Case %d: %lld\n",++kase,ans);
}
}

lightoj 1236 正整数唯一分解定理的更多相关文章

  1. hdu1215 正整数唯一分解定理应用

    B - (例题)因子和 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64 ...

  2. hdu4497 正整数唯一分解定理应用

    C - (例题)整数分解,计数 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65535KB    ...

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

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

  4. LightOJ - 1236 (唯一分解定理)

    题意:求有多少对数对(i,j)满足lcm(i,j) = n,1<=i<=j, 1<=n<=1e14. 分析:根据整数的唯一分解定理,n可以分解为(p1^e1)*(p2^e2)* ...

  5. LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

    http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...

  6. LightOJ - 1341 Aladdin and the Flying Carpet 唯一分解定理LightOJ 1220Mysterious Bacteria

    题意: ttt 组数据,第一个给定飞毯的面积为 sss,第二个是毯子的最短的边的长度大于等于这个数,毯子是矩形但不是正方形. 思路: 求出 sss 的所有因子,因为不可能是矩形,所以可以除以 222, ...

  7. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  8. LightOJ - 1341唯一分解定理

    唯一分解定理 先分解面积,然后除2,再减去面积%长度==0的情况,注意毯子不能是正方形 #include<map> #include<set> #include<cmat ...

  9. Aladdin and the Flying Carpet LightOJ 1341 唯一分解定理

    题意:给出a,b,问有多少种长方形满足面积为a,最短边>=b? 首先简单讲一下唯一分解定理. 唯一分解定理:任何一个自然数N,都可以满足:,pi是质数. 且N的正因子个数为(1+a1)*(1+a ...

随机推荐

  1. typedef std::string AddressLines[4]定义了一个string数组,大小为4

    int main() { typedef std::]; std::]; std::string *pal1 = new AddressLines; delete [] pal; delete [] ...

  2. 在git bush中如何退出vim编辑器

    写npm的pakege.json文件的files配置时,如果有不想包含的文件,那就要创建.npmignore文件排除,但windows系统又不允许创建以点开头命名的文件,咋办? 这时候就要用到linu ...

  3. php 文件操作中几种方法整理

    1.获取文件夹下所有文件个数 echo ShuLiang("../0503lianxi"); function ShuLiang($filename) { if(is_dir($f ...

  4. tomcat 显示目录文件列表

    conf/web.xml中,listings改为true,重启 http://liusu.iteye.com/blog/794613 <servlet> <servlet-name& ...

  5. python入门安装

    一.在python官网上下载python3.4或python2.7版本,这两个版本目前比较稳定,安装第3方包相对而言没有什么问题. 二.安装成功后,在计算机->属性->高级系统设置-> ...

  6. Responder一点也不神秘————iOS用户响应者链完全剖析

    一.事件分类 对于IOS设备用户来说,他们操作设备的方式主要有三种:触摸屏幕.晃动设备.通过遥控设施控制设备.对应的事件类型有以下三种: 1.触屏事件(Touch Event) 2.运动事件(Moti ...

  7. 智能卡安全机制比较系列(三) MPCOS

    MPCOS是金普斯早期推出的一款多应用支付芯片卡操作系统,支持ISO7816以及PCOS的数据格式和命令.MPCOS具有两级目录文件结构,即MF下可以有一级DF,每个DF下最多可创建63个EF. MP ...

  8. Qt 的信号与槽机制介绍(10个要注意的问题)

    QT 是一个跨平台的 C++ GUI 应用构架,它提供了丰富的窗口部件集,具有面向对象.易于扩展.真正的组件编程等特点,更为引人注目的是目前 Linux 上最为流行的 KDE 桌面环境就是建立在 QT ...

  9. windows driver inf文件

    原来修改了inf文件会导致签名过的驱动包哈希值不正确了啊.现在才知道. = = http://www.chiphell.com/thread-827956-1-1.html

  10. SQL语言的组成

    在正式学习SQL语言之前,首先让我们对SQL语言有一个基本认识,介绍一下SQL语言的 组成: 1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义. 2.一个SQL表由行集构成, ...