hdu 1528 Perfection(数论·因子和·C++·java)

2024-03-27 23:18

本文主要是介绍hdu 1528 Perfection(数论·因子和·C++·java),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:http://poj.org/problem?id=1528

Perfection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11909 Accepted: 5595

Description

From the article Number Theory in the 1994 Microsoft Encarta: ``If a, b, c are integers such that a = bc, a is called a multiple of b or of c, and b or c is called a divisor or factor of a. If c is not 1/-1, b is called a proper divisor of a. Even integers, which include 0, are multiples of 2, for example, -4, 0, 2, 10; an odd integer is an integer that is not even, for example, -5, 1, 3, 9. A perfect number is a positive integer that is equal to the sum of all its positive, proper divisors; for example, 6, which equals 1 + 2 + 3, and 28, which equals 1 + 2 + 4 + 7 + 14, are perfect numbers. A positive number that is not perfect is imperfect and is deficient or abundant according to whether the sum of its positive, proper divisors is smaller or larger than the number itself. Thus, 9, with proper divisors 1, 3, is deficient; 12, with proper divisors 1, 2, 3, 4, 6, is abundant."
Given a number, determine if it is perfect, abundant, or deficient.

Input

A list of N positive integers (none greater than 60,000), with 1 <= N < 100. A 0 will mark the end of the list.

Output

The first line of output should read PERFECTION OUTPUT. The next N lines of output should list for each input integer whether it is perfect, deficient, or abundant, as shown in the example below. Format counts: the echoed integers should be right justified within the first 5 spaces of the output line, followed by two blank spaces, followed by the description of the integer. The final line of output should read END OF OUTPUT.

Sample Input

15 28 6 56 60000 22 496 0

Sample Output

PERFECTION OUTPUT15  DEFICIENT28  PERFECT6  PERFECT56  ABUNDANT
60000  ABUNDANT22  DEFICIENT496  PERFECT
END OF OUTPUT
分析:
由于a!=-1 && a!=1所以b是除number本身外的所有因子。完美数就是所有因子的和对number进行素因子分解,分解情况:a1-q1 a2-q2 …… an-qn 那么所有的因子和应该等于:(1+a1+a1^2+……+a1^q1)(1+a2+a2^2+……+a2^q2)……(1+an+an^2+……+an^qn)  对于每一项:1+a+a^2+……+a^n等比数列求和,当:
n是奇数时,乘积等于(1+a^(n/2+1))(1+a+……+a^(n/2)). (奇转偶)
n是偶数时,乘积等于(1+a^(n/2+1))(1+a+……+a^(n/2-1))+a^(n/2).   (偶转奇)
用C++很快写完并成功1A,想到刚学java,于是试着用java仿照着写写看,好家伙,运行时间,内存消耗都增长了不少,而且还莫名的错了好几次。CE因为类名的问题,WA是因为没有及时在输入0时跳出,PE则是System.out.println("PERFECTION OUTPUT\n");   ||-_-

c++:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int fac[300],p[300],top;
void resolve(int x){top=0;memset(p,0,sizeof(p));for(int i=2;i*i<=x;i++){if(x%i==0){fac[top]=i;while(x%i==0){x/=i;p[top]++;}top++;}}if(x>1){fac[top]=x;p[top++]++;}
}
int power(int a,int n){int ans=1,temp=a;while(n){if(n&1) ans=ans*temp;temp=temp*temp;n>>=1;}return ans;
}
int cal(int a,int n){if(n==2) return (1+a+a*a);if(n==1) return (1+a);if(n&1) return (1+power(a,n/2+1))*cal(a,n/2);else return (1+power(a,(n/2+1)))*cal(a,(n/2-1))+power(a,(n/2));
}
int main()
{//freopen("cin.txt","r",stdin);int a;printf("PERFECTION OUTPUT\n");while(cin>>a&&a){resolve(a);int res=1;for(int i=0;i<top;i++){res=res*cal(fac[i],p[i]);}printf("%5d  ",a);res=res-a;if(res<a) puts("DEFICIENT");else if(res==a) puts("PERFECT");else puts("ABUNDANT");}printf("END OF OUTPUT\n");return 0;
}
java:
import java.util.*;
import java.lang.String;
public class Main {static int[] fac=new int [300],p=new int [300];static int top;static void resolve(int x){top=0;Arrays.fill(p,0); //Arrays belong to utilfor(int i=2;i*i<=x;i++){if(x%i==0){fac[top]=i;while(x%i==0){x/=i;p[top]++;}top++;}}if(x>1){fac[top]=x;p[top++]++;}}static int power(int a,int n){int ans=1,temp=a;while(n>0){if(n%2==1) ans=ans*temp;temp=temp*temp;n>>=1;}return ans;}static int cal(int a,int n){if(n==2) return (1+a+a*a);if(n==1) return (1+a);if(n%2==1) return (1+power(a,n/2+1))*cal(a,n/2);else return (1+power(a,(n/2+1)))*cal(a,(n/2-1))+power(a,(n/2));}public static void main(String[] args) {int a;Scanner sc=new Scanner(System.in);System.out.println("PERFECTION OUTPUT");while(sc.hasNextInt()){a=sc.nextInt();if(a==0) break;resolve(a);int res=1;for(int i=0;i<top;i++){res=res*cal(fac[i],p[i]);}String str;res=res-a;if(res<a) str="DEFICIENT";else if(res==a) str="PERFECT";else str="ABUNDANT";System.out.printf("%5d  %s\n",a,str);}System.out.printf("END OF OUTPUT\n");}}
/*class perfect is public, should be declared in a file named perfect.java
public class perfect {^
把perfect 改成Main即可。(public修饰的类名必须和文件名一样)
*/



这篇关于hdu 1528 Perfection(数论·因子和·C++·java)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/853721

相关文章

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推