Factovisors - PC110704
欢迎访问我的新博客:http://www.milkcu.com/blog/
原文地址:http://www.milkcu.com/blog/archives/uva10139.html
原创:
作者:MilkCu
题目描述
Problem D: Factovisors
The factorial function, n! is defined thus for n a non-negative integer:
0! = 1
n! = n * (n-1)! (n > 0)
We say that a divides b if there exists an integer k such that
k*a = b
The input to your program consists of several lines, each containing two non-negative integers, n and m, both less than 2^31. For each input line, output a line stating whether or not m divides n!, in the format
shown below.
Sample Input
6 9
6 27
20 10000
20 100000
1000 1009
Output for Sample Input
9 divides 6!
27 does not divide 6!
10000 divides 20!
100000 does not divide 20!
1009 does not divide 1000!
解题思路
思路比较流畅,依次求2~n之间的数与m的最大公约数,然后用公约数去除m。
若m能被除尽,则m可以整除n!;否则不能。
但是提交到UVaOJ为什么会超时呢?看来还需要优化。
如果在遍历2~n之间的数时,遇到的数i为质数。
若m为素数且m>n,则m与2~n中的任何数互素。
其他优化:把开方放在循环外边。
从n到2递减寻找最大公约数可以提高效率。
还要注意特殊情况的处理。
PC可以通过,UVaOJ总是超时,太浪费时间了。
对于我的不高的要求,以后使用PC吧。
两种求最大公约数的方法:
1. 递归
int gcd(int a, int b) {
if(b == 0) {
return a;
}
if(a < b) {
return gcd(b, a);
}
return gcd(b, a % b);
}
2. 循环
int gcd(int a, int b) {
if(a < b) {
int tmp = a;
a = b;
b = tmp;
}
while(b > 0) {
int tmp = a;
a = b;
b = tmp % b;
}
return a;
}
代码实现
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int isPrime(int x) {
int sq = sqrt(x);
for(int i = 2; i <= sq; i++) {
if(x % i == 0) {
return 0;
}
}
return 1;
}
int gcd(int a, int b) {
if(a < b) {
int tmp = a;
a = b;
b = tmp;
}
while(b > 0) {
int tmp = a;
a = b;
b = tmp % b;
}
return a;
}
int main(void) {
int n, m;
while(cin >> n >> m) {
if(m == 0) {
cout << m << " does not divide " << n << "!" << endl;
continue;
}
if(m == 1) {
cout << m << " divides " << n << "!" << endl;
continue;
}
if(isPrime(m) && m > n) {
cout << m << " does not divide " << n << "!" << endl;
continue;
}
int tm = m;
for(int i = n; i >= 2; i--) {
int g = gcd(i, tm);
if(g > 1) {
//cout << i << " " << g << endl;
tm /= g;
}
if(tm == 1) {
cout << m << " divides " << n << "!" << endl;
break;
}
}
if(tm != 1) {
cout << m << " does not divide " << n << "!" << endl;
}
}
return 0;
}
(全文完)
本文地址:http://blog.csdn.net/milkcu/article/details/23592449
Factovisors - PC110704的更多相关文章
- UVA 10139 Factovisors(数论)
Factovisors The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * ...
- poj 2649 Factovisors 对n!进行因数分解
Factovisors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4431 Accepted: 1086 Descr ...
- Kattis之旅——Factovisors
The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- 开涛spring3(9.3) - Spring的事务 之 9.3 编程式事务
9.3 编程式事务 9.3.1 编程式事务概述 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
- 从“n!末尾有多少个0”谈起
在学习循环控制结构的时候,我们经常会看到这样一道例题或习题.问n!末尾有多少个0?POJ 1401就是这样的一道题. [例1]Factorial (POJ 1401). Description The ...
随机推荐
- OCP读书笔记(26) - 题库(ExamF)
501.Note the output of the following query;SQL> SELECT flashback_archieve_name, status FROM dba_f ...
- 网络资源(2) - Maven视频
2014_08_23 http://v.youku.com/v_show/id_XNDE2NzM0Nzk2.html Maven最佳实践,公司真实环境实践-私服最佳实践 2014_08_24 http ...
- DHot.exe 热点新闻
别人的电脑上的今日插件U菜,打开几个PPT文件,和一个视频文件(默认的音频和视频打开百度),结果突然弹出一个热点广告信息表,形式与风格QQ非常相似,例如下面的附图: 托盘图标: 经过搜索.得到例如以下 ...
- C#后台利用正则表达式查找匹配字符
/// <summary> /// 发送短信 系统固化短信 /// </summary> /// <param name=&qu ...
- Tyvj P1016 包装问题 (DP)
底 Background 太原诚成中学2模拟法庭竞赛 第三条道路 描写叙述 Description 有一个箱子容量为v(正整数.o≤v≤20000).同一时候有n个物品(o≤n≤30).每一个物品有一 ...
- ASP.NET 5 (vNext)
ASP.NET 5 (vNext) 理解和入门 概述 ASP.NET 5 (又称为vNext) 是自ASP.NET产生15年以来一次革命性的更新, 我们可以从以下几点来理解其概貌和意义: ASP. ...
- UIDocumentInteractionController 文件预览
//创建并从底部弹出来 - (void)viewDidLoad { [super viewDidLoad]; [self setupDocumentControllerWithURL:fileURL] ...
- 基OOP知识
从今天开始,我开始总结GAO还通高老师<android道路的建筑师>,尝试一个星期写三个博客. 相对而言.看到这篇文章有点速度比你可以观看视频,刚才看的视频是更具体的.假设有兴趣,跟着我去 ...
- [CLR via C#]1.1 将源代码编译成托管代码
原文:[CLR via C#]1.1 将源代码编译成托管代码 1. 公共语言运行时(Common Language Runtime,CLR)是一种可由多种编程语言使用的"运行时". ...
- SICP 锻炼 (1.45)解决摘要
SICP 1.45是对前面非常多关于不动点的习题的总结. 题目回想了我们之前在1.3.3节使用的不动点寻找方法.当寻找y -> x/y 的不动点的时候,这个变换本身不收敛.须要做一次平均阻尼才干 ...