Factors and Factorials 

The factorial of a number N (written N!) is defined as the product of all the integers from 1 to N. It is often defined recursively as follows:

Factorials grow very rapidly--5! = 120, 10! = 3,628,800. One way of specifying such large numbers is by specifying the number of times each prime number occurs in it, thus 825 could be specified as (0 1 2 0 1) meaning no twos, 1 three, 2 fives, no sevens and 1 eleven.

Write a program that will read in a number N (  ) and write out its factorial in terms of the numbers of the primes it contains.

Input

Input will consist of a series of lines, each line containing a single integer N. The file will be terminated by a line consisting of a single 0.

Output

Output will consist of a series of blocks of lines, one block for each line of the input. Each block will start with the number N, right justified in a field of width 3, and the characters `!', space, and `='. This will be followed by a list of the number of times each prime number occurs in N!.

These should be right justified in fields of width 3 and each line (except the last of a block, which may be shorter) should contain fifteen numbers. Any lines after the first should be indented. Follow the layout of the example shown below exactly.

Sample input

  1. 5
  2. 53
  3. 0

Sample output

  1. 5! = 3 1 1
  2. 53! = 49 23 12 8 4 4 3 2 2 1 1 1 1 1 1
  3. 1
  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4. bool isprime(int a)
  5. {
  6. int i;
  7. for (i = 2; i*i <= a;i++)
  8. if (a%i == 0)
  9. return false;
  10. return true;
  11. }
  12. int prime[100], count[100];
  13. int main()
  14. {
  15. int n;
  16. int i,num;
  17. for (i = 2, num = 0; i <= 100;i++)
  18. if (isprime(i))
  19. {
  20. prime[num++] = i;
  21. }
  22.  
  23. while (scanf("%d", &n) == 1 && n)
  24. {
  25. memset(count,0,sizeof(count));
  26. int maxn = 0;
  27. for (i = 2; i <= n; i++)
  28. {
  29. int m = i;
  30. int j;
  31. for (j = 0; j < num; j++)
  32. {
  33. while (m%prime[j] == 0)
  34. {
  35. m = m / prime[j];
  36. count[j]++;
  37. if (j>maxn)
  38. maxn = j;
  39. }
  40. }
  41. }
  42. printf("%3d! =", n);
  43. for (i = 0; i <= maxn; i++)
  44. {
  45. if (i == 15)
  46. printf("\n ");
  47. printf("%3d", count[i]);
  48. }
  49.  
  50. printf("\n");
  51. }
  52. return 0;
  53. }

  这道题应该特别注意输出格式。

UVA 160 - Factors and Factorials的更多相关文章

  1. UVA 1575 Factors

    https://vjudge.net/problem/UVA-1575 题意: 令f(k)=n 表示 有n种方式,可以把正整数k表示成几个数的乘积的形式. 例 10=2*5=5*2,所以f(10)=2 ...

  2. [UVA160]Factors and Factorials 题解

    前言 这道题目本身毫无技术含量珂言,但是输出格式珂以调一年 题解 这道题让我们求\(N!\)中每个质数的个数. 一种方法是直接模拟,枚举\(N!\)中的每个元素,然后暴力查看每个数含有有多少质数. 但 ...

  3. Zerojudge解题经验交流

    题号:a001: 哈囉 背景知识:输出语句,while not eof 题号:a002: 簡易加法 背景知识:输出语句,while not eof,加法运算 题号:a003: 兩光法師占卜術 背景知识 ...

  4. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  5. \r\n和\n的区别

    写Java代码的时候习惯用\r\n换行,这样可移植性比较好但是,在UVa - 160中就出现了错误,来看看是为什么吧. http://bbs.csdn.net/topics/220033879

  6. UVA 10699 Count the factors 题解

    Time limit 3000 ms OS Linux Write a program, that computes the number of different prime factors in ...

  7. uva 129 krypton factors ——yhx

     Krypton Factor  You have been employed by the organisers of a Super Krypton Factor Contest in which ...

  8. UVa 884 - Factorial Factors

    题目:输出n!中素数因数的个数. 分析:数论.这里使用欧拉筛法计算素数,在计算过程中求解就可以. 传统筛法是利用每一个素数,筛掉自己的整数倍: 欧拉筛法是利用当前计算出的全部素数,乘以当前数字筛数: ...

  9. UVa 11621 - Small Factors

    称号:发现没有比给定数量少n的.只要2,3一个因素的数字组成. 分析:数论.贪婪,分而治之. 用两个三分球,分别代表乘法2,和繁殖3队列,队列产生的数字,原来{1}. 然后.每取两个指针相应元素*2和 ...

随机推荐

  1. Java进阶专题(二十六) 将近2万字的Dubbo原理解析,彻底搞懂dubbo

    前言 ​ 前面我们研究了RPC的原理,市面上有很多基于RPC思想实现的框架,比如有Dubbo.今天就从Dubbo的SPI机制.服务注册与发现源码及网络通信过程去深入剖析下Dubbo. Dubbo架构 ...

  2. 了解 Vue 的 Compsition API

    在这篇文章中,我将讲讲 Vue 的 Composition API 为什么比之前的 Options API 要好,以及它是如何工作的. Options API 有什么问题 首先,这里不是要大家放弃 O ...

  3. RabbitMQ 入门 (Go) - 5. 使用 Fanout Exchange 做服务发现(下)

    到目前为止,我一直专注于如何让消息进出消息代理,也就是RabbitMQ. 实际上,我们可以继续使用 RabbitMQ 和它的 Exchanges 来连接这个应用程序的其他部分,但是我想探索一个稍微不同 ...

  4. 原生对象写法,dom调用方法

    1 var App = (function () { 2     var App = function () { 3 //全局变量 4         this.init(); 5 this.a = ...

  5. (十五)struts2的文件上传和下载

    文件上传的原理 我们以前学习上传的时候知道需要将表单的enctype属性设置为multipart/form-data. 表单的enctype属性指定的是表单数据的编码方式,有三个值: -applica ...

  6. Leedcode算法专题训练(数学)

    204. 计数质数 难度简单523 统计所有小于非负整数 n 的质数的数量. class Solution { public int countPrimes(int n) { boolean[] is ...

  7. 数据库MySQL六

    介绍什么是JDBC JAVA SE也有 提高综合篇 JDBC(Java Database Connectivity) :java和数据库的连接技术,sun公司推出的一套java应用程序访问数据库的技术 ...

  8. win10美化,让你的win10独一无二,与众不同!

    2020.06.23 更新 1 原则 美化之前,得先有一个目标对不对,笔者是一个喜欢简单的人,因此美化本着三大原则:简单,干净,整洁. 呃....好像很抽象的样子,上图吧.反正没图没真相. 怎么样,还 ...

  9. (二) LDAP 安装

    LDAP 安装 参考:https://blog.51cto.com/bigboss/2341986

  10. Knight Moves UVA - 439

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...