codevs 4712 gcd与lcm问题

这个是某年noip什么题的加强版。

并无卵用?线性筛下质因子个数即可。然后答案就是2^(m/d的质因子个数)

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1000005
using namespace std;
int t,m,d;
int prime[maxn/10],cnt=0,tab[maxn],r[50];
bool vis[maxn];
void get_table()
{
tab[1]=1;
memset(vis,false,sizeof(vis));
for (int i=2;i<maxn;i++)
{
if (vis[i]==false)
{
prime[++cnt]=i;
tab[i]=2;
}
for (int j=1;j<=cnt && i*prime[j]<maxn;j++)
{
vis[i*prime[j]]=true;
if (i%prime[j]==0)
{
tab[i*prime[j]]=tab[i];
break;
}
else
tab[i*prime[j]]=tab[i]+1;
}
}
int ans=1;
for (int i=1;i<=35;i++)
{
ans=ans*2;
r[i]=ans;
}
}
int main()
{
scanf("%d",&t);
get_table();
for (int k=1;k<=t;k++)
{
scanf("%d%d",&d,&m);
if ((d==0) || (m==0)) printf("0\n");
else if (m<d) printf("0\n");
else if (m%d!=0) printf("0\n");
else printf("%d\n",r[tab[m/d]-1]);
}
return 0;
}

时间: 2024-08-07 04:13:45

codevs 4712 gcd与lcm问题的相关文章

hdu 4497 GCD and LCM

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1092    Accepted Submission(s): 512 Problem Description Given two positive integers G and L, could you tell me how many solutions of (

hdu 4497 GCD and LCM 数论 素数分解

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 607 Problem Description Given two positive integers G and L, could you tell me how many solutions of

hdu 4497 GCD and LCM 数学

GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4497 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

gcd,lcm,ext_gcd,inv

Least Common Multiple http://acm.hdu.edu.cn/showproblem.php?pid=1019 1 #include<cstdio> 2 int gcd(int a,int b){ 3 return b?gcd(b,a%b):a; 4 } 5 int lcm(int a,int b){ 6 return a/gcd(a,b)*b; 7 } 8 int main(){ 9 int n,m,ans,x; 10 while(~scanf("%d&q

POJ 2429 GCD &amp;amp; LCM Inverse (大数分解)

GCD & LCM Inverse 题目:http://poj.org/problem? id=2429 题意: 给你两个数的gcd和lcm,[1, 2^63). 求a,b.使得a+b最小. 思路: lcm = a * b / gcd 将lcm/gcd之后进行大数分解.形成a^x1 * b^x2 * c^x3-- 的形式.当中a,b,c为互不同样的质数.然后暴力枚举就可以. 代码: #include<map> #include<set> #include<queue&

hdu 4497 GCD and LCM(唯一分解+容斥原理)

GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 78 Accepted Submission(s): 43 Problem Description Given two positive integers G and L, could you tell me how many solutions of (x, y, z)

hdu 4497 GCD and LCM(排列组合)

题目:hdu 4497 GCD and LCM 题目大意:给出三个数的最大公约数,和最小公倍数,问这三个数的排列组合关系. 解题思路:最小公倍数/最大公约数 ==  三个数不同部分的乘积.这样来考虑的话,三个数都要有最大公约数的部分,其余的部分就是由LCM / GCD 里面的因子构成.这里面的因子可能会有 2 2 3 这样的情况, 不同的因子之间是不会相互干扰的,但是相同的会出现问题,因为,不能同时将相同的因子都放在三个位置上,这样最大公约数就的要乘上这个因子.然后对于单种因子来考虑的话,每种因

HDU4497 GCD and LCM 数论 素数分解

题意很简单首先以前做最简单的LCM跟CGD的时候都知道先求出两个数A,B的最大公约数GCD,那么LCM可以利用  A*B/GCD来求得,这点一开始脑残了没想到,结果没有进行特盘所以错了,意思就是 题目给的L%G不为0的话就是无解,结果我给判其它的去了,肯定漏了些什么没有发现 然后对于 L/G进行素因子分解,同时任意的数都能够通过素因子分解来表示,所以三个解x,y,z也能分解 L/G = p1^q1*p2^q2.... x = p1^i1*... y = p1^j1*... z = p1^k1*.

理论: 数论(1):整除、gcd以及lcm

整除 整除的性质 设a, b是两个整数, 并且b ≠ 0. 如果存在整数c, 使得 a = b * c , 则称a被b整除, 或者b整除a,记作b |a(这里是a 被 b整除, a >= b) 此时又称a是b的倍数, b是a的因子.如果b不整除a, 记作 · 整除基本定义 定义1.1:如果n被2除的余数为 0, 则对于某个整数k, 有n = 2k, 我们称n为偶数:而如果n被2除的余数为1, 我们则对于某个整数k, 有n = 2k + 1, 我们称n为奇数. 定义1.2 :设a, b是两个整数,