C - (例题)整数分解,计数

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
 

Input

First line comes an integer T (T <= 12), telling the number of test cases.
The next T lines, each contains two positive 32-bit signed integers, G and L.
It’s guaranteed that each answer will fit in a 32-bit signed integer.
 

Output

For each test case, print one line with the number of solutions satisfying the conditions above.
 

Sample Input

2
6 72
7 33
 

Sample Output

72
0
题目大意:
给你两个数L,G,问你有多少有序数组(x,y,z)满足GCD(x,y,z)=G,LCM(x,y,z)=L,首先如果gcd(x,y,z)=G,
那么有gcd(x/G,y/G,z/G)=1(说明这三个数两两互素),此时应该满足lcm(x,y,z)=L/G,要求L/G为整数,则若L%G==0,则一定有解,(x,y,z都等于L/G即可)
反之无解
此时将L/G作正整数唯一分解,T=L/G=a1^b1*a2^b2*.......*an^bn,对于a1,要满足gcd(x/g,y/g,z/g)=1,a1^k则至少有一个k=0,同时
还得满足lcm(x/g,y/g,z/g)=l/g,则至少有一个k=b1,这样就有三种情况(0,0,b1)(b1,b1,0)(0,1~b1-1,b1)共有6+6(b1-1)=6*b1种,其他的同理
由分步乘法计数原理,最终答案为(6*b1)*(6*b2)*........(6*bn)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include<algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=2e5;//
bool vis[maxn];
ll prime[maxn/];
int tot;
void getprime()//因为n的范围是1e14,打表只需要打到sqrt(n)即可,最多只可能有一个素因子大于sqrt(n),最后特判一下即可;
{
memset(vis,true,sizeof(vis));
tot=;
for(ll i=;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=0;
for(int i=2;i<maxn;i++)
{
if(vis[i]) prime[tot++]=i;
for(int j=0;j<tot&&prime[j]*i<maxn;j++)
{
vis[i*prime[j]]=false;
if(i%prime[j]==0) 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 pow_mod(int m,int n)
{
ll pw=;
while(n)
{
if(n&) pw*=m;
m*=m;
n/=;
}
return pw;
}
int kase;
int main()
{
int T;
ll L,G;
getprime();
scanf("%d",&T);
kase=;
while(T--)
{
scanf("%lld%lld",&G,&L);
if(L%G) {printf("0\n");continue;}
ll n=L/G;
sbreak(n);
ll sum=;
for(int i=;i<cnt;i++)
{
sum*=(*b[i]);
}
printf("%lld\n",sum);
}
}

hdu4497 正整数唯一分解定理应用的更多相关文章

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

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

  2. lightoj 1236 正整数唯一分解定理

    A - (例题)整数分解 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:32768KB     6 ...

  3. NOIP2009Hankson 的趣味题[唯一分解定理|暴力]

    题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...

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

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

  5. UVa 10791 Minimum Sum LCM【唯一分解定理】

    题意:给出n,求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小 看的紫书--- 用唯一分解定理,n=(a1)^p1*(a2)^p2---*(ak)^pk,当每一个(ak)^pk作为一个单 ...

  6. 简单数论之整除&质因数分解&唯一分解定理

    [整除] 若a被b整除,即a是b的倍数,那么记作b|a("|"是整除符号),读作"b整除a"或"a能被b整除".b叫做a的约数(或因数),a ...

  7. 唯一分解定理(以Minimun Sum LCM UVa 10791为例)

    唯一分解定理是指任何正整数都可以分解为一些素数的幂之积,即任意正整数n=a1^p1*a2^p2*...*ai^pi:其中ai为任意素数,pi为任意整数. 题意是输入整数n,求至少2个整数,使得它们的最 ...

  8. hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  9. hdu1215-七夕节-(埃氏筛+唯一分解定理)

    七夕节 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. DeDe调用指定栏目ID下的文章

    *注: row: 调用条数 titlelen: 字数 typeid: 调用的栏目ID orderby: 按照升序对记录进行排序 idlist: 提取特定文档为空 infolen='40' 内容简介长度 ...

  2. Python番外之 阻塞非阻塞,同步与异步,i/o模型

    1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调用方式: 同步/异步主要针对C端: 同步:      所谓同步,就 ...

  3. js运算符的优先级

    自上向下优先级降低 运算符 描述 . [] () 字段访问.数组下标.函数调用以及表达式分组 ++ -- - ~ ! delete new typeof void 一元运算符.返回数据类型.对象创建. ...

  4. documentElement vs body

    document.documentElement与document.body. 2011-03-12 10:15:46|  分类: javascript|字号 订阅     这是DOMDocument ...

  5. Http Authentication Java

    http://docs.oracle.com/javase/7/docs/technotes/guides/net/http-auth.html Http Authentication Overvie ...

  6. LeetCode_sqrt(x)

    class Solution { public: int sqrt(int x) { // Start typing your C/C++ solution below // DO NOT write ...

  7. 2015必须要看的APP源码

    多媒体类型 哔哩哔哩(bilibili)客户端源码 一个高仿哔哩哔哩(bilibili)客户端的开源项目,效果不错 下载地址: http://www.apkbus.com/forum.php?mod= ...

  8. java集合经常出现空指针问题的解决方案

    我自己问自己这个问题的时候都把自己给雷住了,但是现实如此,经常写的程序就是出现空指针的错误. 如: public List add(){ List list=null; try { list=new ...

  9. js深入研究之自定义混合Mixin函数

    <script type="text/javascript"> /* 增加函数 */ function augment(receivingClass, givingCl ...

  10. Linux企业级项目实践之网络爬虫(1)——项目概述及准备工作

    我们在学习了Linux系统编程之后,需要一些实战项目来提高自己的水平,本系列我们通过编写一个爬虫程序,将我们学习的知识进行综合应用,同时在实现项目的过程中逐渐养成一些有用的思维方式,并具有初步的软件开 ...