lightoj 1215
lightoj 1215 Finding LCM
链接:http://www.lightoj.com/volume_showproblem.php?problem=1215
题意:已知 a, b, l 和 lcm(a, b, c) = l ,求最小的 c 的值。
思路:先找 l 的素因子并判断此因子是否为 a, b 的素因子,如果是,则判断他们各自的欧拉值的大小。因为 c 最大可能等于 l 的值,所以刚开始先把 l 的值赋给 c 。
当 l 中的某个素因子的欧拉值(lr1)大于 a,b 中相同的素因子的欧拉值(ar1, br1)时,c中肯定含有次素因子并且欧拉值(cr1 >= lr1 ),然而 c 是 l 的因子,所以(cr1 <= lr1 ), 所以 cr1 == lr1 ,不用进行处理。
当 l 中的某个素因子的欧拉值(lr1)等于(最大就是等于,不可能小于) a,b 中相同的素因子的欧拉值(ar1, br1)时,c中不一定含有次素因子,当 c 要取最小时,就可以不含有这个素因子,所以就把 c 中的 此素因子除干净。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; typedef long long LL;
const int N = ;
bool tag[N];
int prime[];
int k = ;
LL a, b ,c, l; LL stein(LL a, LL b) //stein 公式求最大公约数
{
if(a < b) swap(a, b);
if(!b) return a;
if(!(a&) && !(b&)) return * stein(a>>, b>>);
if(!(a&)) return stein(a>>, b);
if(!(b&)) return stein(a, b>>);
return stein((a-b)>>, b);
}
//当然,你也可以用欧几里得
LL gcd(LL a, LL b)
{
return b ? gcd(b, a%b) : a;
} int euler(LL *n, int m) //计算欧拉函数: 此函数中 n 值的变化会传回原处
{
int e = ;
while(!((*n) % m))
e++, (*n) /= m;
return e;
} void prm() //素数表(线性素数筛法)
{
int i,j;
memset(tag, , sizeof(tag));
tag[] = , prime[k++] = ;
for(i = ; i < N; i += )
{
if(!tag[i]) prime[k++] = i;
for(j = ; j < k && i*prime[j] < N; ++j)
{
tag[i*prime[j]] = ;
if(i%prime[j] == ) break;
}
}
} void ct(int q) //计算c值
{
int i, cnta, cntb, cntL, cnt0, cs = ;
c = l; // c 最大可能等于c, 先赋值为c,遇到情况在做除法减小它
if(l % (a/stein(a,b)*b)) //不可能的情况:l 不是(a, b)的最小公倍数的倍数
{
printf("Case %d: impossible\n", q);
return;
}
for(i=; i < k && (prime[i] <= a || prime[i] <= b); i++) //
{
cnta = cntb = ;
if(!(l%prime[i])) //是某个素数倍数的时候
{
cntL = euler(&l, prime[i]); // l 部分欧拉函数值
if(!(a%prime[i])) // 当这个素数也是a 的因子的时候
cnta = euler(&a, prime[i]); // a 的部分欧拉函数值
if(!(b%prime[i])) //当这个素数也是b 的因子的时候
cntb = euler(&b,prime[i]); //同 a 的操作
cnt0 = cnta > cntb ? cnta : cntb; // 最小公倍数当然是取值较大欧拉值
/*
a,b里边因子的欧拉值肯定要小于等于l的相同的因子的欧拉值的,当相等时,c要取最小就必须不含此因子
当a, b 中的因子的欧拉值都小于l中的时,c中相同因子的欧拉值必须大于等于l中的值,最小当然取等于啦
这也是为什么下面的只处理等于的情况
*/
if(cntL == cnt0)
while(cnt0--)
c /= prime[i];
}
}
if(a > && euler(&l, a) <= ) c /= a; //当 a 中含有大于1000的素数时的处理a
if(b > && euler(&l, b) <= ) c /= b; //当 b 中含有大于1000的素数时的处理b
printf("Case %d: %lld\n", q, c);
} int main()
{
int t, q=;
prm();
scanf("%d", &t);
while(t--)
{
scanf("%lld%lld%lld", &a, &b, &l);
ct(q++);
}
return ;
}
lightoj 1215的更多相关文章
- Finding LCM LightOJ - 1215 (水题)
这题和这题一样......只不过多了个数... Finding LCM LightOJ - 1215 https://www.cnblogs.com/WTSRUVF/p/9316412.html #i ...
- LightOj 1215 - Finding LCM(求LCM(x, y)=L中的 y )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1215 题意:已知三个数a b c 的最小公倍数是 L ,现在告诉你 a b L 求最 ...
- LightOj 1215 Finding LCM
Discription LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, ...
- LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- LightOj 1298 - One Theorem, One Year(DP + 欧拉)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1298 题意:给你两个数 n, p,表示一个数是由前 k 个素数组成的,共有 n 个素数 ...
- 1214 - Large Division -- LightOj(大数取余)
http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一 ...
- LightOJ Beginners Problems 部分题解
相关代码请戳 https://coding.net/u/tiny656/p/LightOJ/git 1006 Hex-a-bonacci. 用数组模拟记录结果,注意取模 1008 Fibsieve's ...
- LightOJ 1341 唯一分解定理
Aladdin and the Flying Carpet Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld &a ...
随机推荐
- Python Tkinter-Event
1.点击 from tkinter import * root=Tk() def printCoords(event): print(event.x,event.y) bt1=Button(root, ...
- PSP1130
PSP时间图: 类型 任务 开始时间 结束时间 净时间 中断时间 日期 开会 开会 16:17 16:50 33 0 20171027 开会 开会 17:00 17:22 22 0 20171028 ...
- 中国剩余定理---FZU 1402 猪的安家
J - 猪的安家 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 期中HTML代码及技术博客
<!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- 周总结<6>
周次 学习时间 新编写代码行数 博客量(篇) 学到知识点 13 10 100 2 网页设计:邻接矩阵深度以及广度遍历
- erlang随机排列数组
参考karl's answer 1> L = lists:seq(1,10). [1,2,3,4,5,6,7,8,9,10] Associate a random number R with e ...
- CCF——相邻数对201409-1
问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1. 输入格式 输入的第一行包含一个整数n,表示给定整数的个数. 第二行包含所给定的n个整数. 输出格式 输出一个整数,表示值正好 ...
- Mware中CentOS设置静态IP
Mware中CentOS设置静态IP 因为之前搭建的MongoDB分片没有采用副本集,最近现网压力较大,所以准备研究一下,于是在自己电脑的虚拟机中搭建环境,但是发现之前VMware设置的是DHCP ...
- Socket 传一幅图片给另一个终端
练习Socket传文件,先添加一个组件,简化socket发送和接收文件, 获取IP和端口的类 public static class AddressHelper { /// <summary&g ...
- SQL入门之多表查询
如果查询需要针对两个或者更多个表,则在需要涉及到表的连接操作(join). 0.笛卡儿积 最简单的连接方式是直接在from子句中加入两个表,并且用join操作符隔开.形式为Table1 join Ta ...