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. boost

    参考博客 http://www.cnblogs.com/lidabo/p/3805487.html http://www.cppblog.com/Robertxiao/archive/2013/01/ ...

  2. [Android分享] 如何解决Android 5.0中出现的警告:Service Intent must be explicit

    Android 5.0程序运行报Service Intent must be explicit错误,原因是5.0的service必须显式调用 改成 Intent intent = new Intent ...

  3. Java语言实现简单FTP软件------>FTP软件效果图预览之下载功能(二)

    介绍完FTP协议后,来看看该软件完成后的效果图 客户端的主界面如上图所示,主要是分为以下几个界面: 数据输入界面:用来让用户输入服务器的地址,用户名,密码,端口号等. 站点菜单.本地菜单.远程菜单以及 ...

  4. Java系列--第三篇 基于Maven的Android开发CAIO

    学习要打好基础,这里用一个项目来学习一下Android的组件,参考网址为这个但不限于这个.有些东西的学习,理解三遍理论还不如一遍操作,所谓理论来自实践,实践是检验真理的唯一标准.所以,虽然看懂了那篇文 ...

  5. checkbox的问题整理

    之前做过一些checkbox的小功能,由于水平有限总是在这一部分花费一些时间,趁现在时间充裕些,整理出来. checkbox是否被选中的判断 第一种: $(this).is(":checke ...

  6. Linux修改时间时区并在Tomcat中生效

    Linux查看当前时间时区linux:~ # datelinux:~ # date –Rlinux:~ # zdump -v /usr/share/zoneinfo/Asia/Beijing ---- ...

  7. JS、CSS兼容性问题的几点总结

    javascript和CSS在不同浏览器下的兼容性问题的几点总结: Javascript部分 1. document.form.item 问题问题:代码中存在 document.formName.it ...

  8. jsonp跨域请求学习笔记

    前言 ajax,用苍白的话赞扬:很好. 我们可以使用ajax实现异步获取数据,减少服务器运算时间,大大地改善用户体验:我们可以使用ajax实现小系统组合大系统:我们还可以使用ajax实现前端的优化.( ...

  9. “#ifdef __cplusplus extern "C" { #endif”的定义

    平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #end ...

  10. pure.css 学习记录

    兼容性记录: IE 8+ Latest Stable: Firefox, Chrome, Safari iOS 6-8 Android 4.x 处理兼容性 <!--[if lte IE 8]&g ...