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. CODEVS 1287 矩阵乘法

    1287 矩阵乘法  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 小明最近在为线性代数而头疼,线性代数确实很抽象(也很无聊) ...

  2. jQuery1.9(辅助函数)学习之——.serialize();

    $("form").serialize();  返回一个String 描述: 将用作提交的表单元素的值编译成字符串,这个方法不接受任何参数. .serialize(); 方法使用标 ...

  3. 个性A标签

    问题: 前段时间,小琳同学问我A标签为啥alert出来的是它的href? 示例: 先来两个标签比较一下. <a id="a" href="http://www.ba ...

  4. 导入IP安全策略图解

    导入IP安全策略图解 点击“开始菜单”→点击“运行”→输入gpedit.msc并回车 →点击“计算机配置”→“windows设置”→“安全设置”,用鼠标右键点击“IP安全策略”,在弹出菜单中点击“所有 ...

  5. MySQL查询指定时间的数据

    user_event :用户事件表 create_time :表中存储时间的字段 #获取当月数据 SELECT * FROM user_event WHERE DATE_FORMAT(create_t ...

  6. Python中TKinter模块中的Label组件

    Python2.7.4      OS—W7x86 1. 简介 Label用于在指定的窗口中显示文本和图像.最终呈现出的Label是由背景和前景叠加构成的内容. Label组件定义函数:Label(m ...

  7. Swift—计算属性-备

    计算属性本身不存储数据,而是从其他存储属性中计算得到数据. 计算属性概念: 计算属性提供了一个getter(取值访问器)来获取值,以及一个可选的setter(设置访问器)来间接设置其他属性或变量的值. ...

  8. Lintcode--007(不同的子序列)

    题目:http://www.lintcode.com/zh-cn/problem/distinct-subsequences/ 2016-08-25 给出字符串S和字符串T,计算S的不同的子序列中T出 ...

  9. 关于linux定时操作cron的理解

    cron是服务名称,crond是后台进程(有的后台也叫作cron,本人是ubuntu12.04,32bit),crontab则是定制好的计划任务表. 软件包安装: 要使用cron服务,先要安装vixi ...

  10. PHP安装OPENSSL扩展模块

    新项目上线时,PHP开发同事反映邮件功能不能正常使用. 原来是用465的SMTP加密端口,不是25端口.那要为当前的PHP安装OPENSSL扩展啦. 还好,网上有很多,弄一个过来就搞定. http:/ ...