抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
素数判断:
一、根据素数定义,该数除了1和它本身以外不再有其他的因数。
详见代码。
int prime()
{
for (int i=; i*i<=n; i++)
{
if (n%i==) //不是素数
return ; //返回1
}
return ; //是素数返回0
}
二、打表,将所有的素数一一列出,存在一个数组里。
详见代码。
void prime()
{
for (int i=; i<; i++) //从2开始一个一个找
{
if (hash[i]==) //这一个判断可以减少很多重复的,节省很多时间
{
for (int j=; i*j<; j++) //只要乘以i就一定不是素数
{
hash[i*j]=; //不是素数标记为1
}
}
}
}
提供一种技巧、如果题目里面所有的计算都是素数之间的转化的话、可以如下。
void prime()
{
int k=;
for (int i=; i<; i++)
{
if (hash[i]==)
{
sushu[k++]=i; //所有的素数都存在了sushu的数组里面,或者放在队列里面q.push(i);
for (int j=; i*j<; j++)
{
hash[i*j]=;
}
}
}
}
举个例子:hdu2710 Max Factor
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2710
Max Factor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4168 Accepted Submission(s):
1366
labels each of his N (1 <= N <= 5,000) cows with a distinct serial number
in the range 1..20,000. Unfortunately, he is unaware that the cows interpret
some serial numbers as better than others. In particular, a cow whose serial
number has the highest prime factor enjoys the highest social standing among all
the other cows.
(Recall that a prime number is just a number that has no
divisors except for 1 and itself. The number 7 is prime while the number 6,
being divisible by 2 and 3, is not).
Given a set of N (1 <= N <=
5,000) serial numbers in the range 1..20,000, determine the one that has the
largest prime factor.
* Lines 2..N+1:
The serial numbers to be tested, one per line
there are more than one, output the one that appears earliest in the input
file.
题目大意:找到所给数的最大素因子,然后在比较这些素因子的大小,找到最大的,最后输出原有的那个数。
详见代码。
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int hash[]; void prime()
{
for (int i=; i<; i++)
{
if (hash[i]==)
{
for (int j=; i*j<; j++)
{
hash[i*j]=i;//i表示的是最大的素因子
}
}
}
//return hash[n];
} int main ()
{
int T;
memset(hash,,sizeof(hash));
sushu();
while (~scanf("%d",&T))
{
int Max=,x=;
while (T--)
{
int n;
scanf("%d",&n);
if (hash[n]>Max)
{
Max=hash[n];
x=n;
}
}
printf ("%d\n",x);
}
return ;
}
最大公约数(gcd)
详见代码。
int gcd(int a,int b)
{
return a%b?gcd(b,a%b):b;
}
最小公倍数
求解最小公倍数,一般都要借助最大公约数。辗转相除求得最大公约数,再用两数之积除以此最大公约数,得最小公倍数。
注意基本!!!
抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)的更多相关文章
- HDU-2710 Max Factor
看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...
- hdu2710 Max Factor
题目 //下面这个是最先用的方法,因为学姐先讲完这个,所以懒得写代码,就将就着这个用,结果搞了老半天,还是错了,心累.. #include<stdio.h> #include<str ...
- Max Factor(素数筛法)题解
Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 12--c完数/最大公约数/最小公倍数/素数/回文数
完数/最大公约数/最小公倍数/素数/回文数 2015-04-08 10:33 296人阅读 评论(0) 收藏 举报 分类: C/C++(60) 哈尔滨工业大学(8) 版权声明:本文为博主原创文章 ...
- poj 3048 Max Factor(素数筛)
这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...
- HDU 2710 Max Factor(数论,素数筛法)
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> usin ...
- ACM Max Factor
To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) co ...
- POJ3048 Max Factor
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
随机推荐
- VMbox复制虚拟机后网卡问题-bring up interface eth0:Device eth0 does not seem to be present
1.使用 ifconfig -a 查看mac地址 eg:HWaddr:08:00:29:B2:2B 2.vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ...
- overflow:scroll 在ios 滚动卡顿
使用 -webkit-overflow-scrolling 属性控制元素在移动设备上是否使用滚动回弹效果. 值 auto 使用普通滚动, 当手指从触摸屏上移开,滚动会立即停止. touch 使用具有回 ...
- C++除法运算 // 静态断言
1.C++中"/"运算:对两个整数做除法,结果仍为整数,如果它的商包含小数部分,则小树部分会被截除. C++ Primer 第五章 P130 2.静态断言(static_asser ...
- Codeforces Round #510 Div. 2 Virtual Participate记
这场打的顺手到不敢相信.如果不是vp的话估计肯定打不到这个成绩. A:最大显然,最小的话每次暴力给最小的+1. #include<iostream> #include<cstdio& ...
- 【题解】CF#403 D-Beautiful Pairs of Numbers
这题还挺对胃口的哈哈~是喜欢的画风!回家路上一边听歌一边想到的解法,写出来记录一下…… 首先,由于 \(b_{k} < a_{k + 1}\) ,所以我们可以看作是在一个长度为 n 的序列上选择 ...
- [BZOJ3380] [USACO2004 Open]Cave Cows 1 洞穴里的牛之一
Description 很少人知道其实奶牛非常喜欢到洞穴里面去探险. 洞窟里有N(1≤N≤100)个洞室,由M(1≤M≤1000)条双向通道连接着它们.每对洞室间 至多只有一条双向通道.有K( ...
- Linux环境下用Weblogic发布项目【三】 -- 启动、登陆、停止WebLogic
一.启动WebLogic: 1.启动前,修改访问端口.IP地址方法: 在config.xml中修改,具体路径如下: /root/Oracle/Middleware/user_projects/doma ...
- IDEA批量修改变量快捷键
Window: Ctrl+Shift+Alt+J Mac: Ctrl+Option+G
- find_in_set 函数使用方法
find_in_set 函数使用方法 个例子来说: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点.4图文...1,12,13 等等 . 现在有篇文章他既是 头条,又是 ...
- udhcpd源码分析2--读取配置文件
1:重要的结构体 读取配置文件信息到全局的结构体struct server_config_t server_config中,这个结构在很多文件中都有引用到很重要. /* dhcpd.h */ stru ...