1035 - Intelligent Factorial Factorization

Given an integer N, you have to prime factorize N! (factorial N).

Input

Input starts with an integer T (≤ 125), denoting the number of test cases.

Each case contains an integer N (2 ≤ N ≤ 100).

Output

For each case, print the case number and the factorization of the factorial in the following format as given in samples.

Case x: N = p1 (power of p1) * p2 (power of p2) * ...

Here x is the case number, p1, p2 ... are primes in ascending order.

Sample Input

Output for Sample Input

3

2

3

6

Case 1: 2 = 2 (1)

Case 2: 3 = 2 (1) * 3 (1)

Case 3: 6 = 2 (4) * 3 (2) * 5 (1)

Notes

The output for the 3rd case is (if we replace space with '.') is

Case.3:.6.=.2.(4).*.3.(2).*.5.(1)

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 200

using namespace std;
int n, k, id;
int a[N], b[N], prime[N], vis[N];

void init()///素数表
{
for(int i = 2; i < N; i++)
{
if(!vis[i])
{
prime[k++] = i;

for(int j = i + i; j < N; j += i)
vis[j] = 1;
}
}

}

void input(int n)
{
id = 0;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));

for(int i = n; i >= 1; i--)
{
int tmp = i, t;

for(int j = 0; j < k && prime[j] <= n; j++)
{
t = 0;

while( tmp % prime[j] == 0)
{
t++;
tmp /= prime[j];
}

if(t != 0)
{
if(a[prime[j]] == 0)///如果该素数没有出现过,就把他放入数组b中。
b[id++] = prime[j];

a[prime[j]] += t;///把素数的个数存在a数组中。
}
}
}
}
void deal()
{
printf("%d = ", n);

sort(b, b+id);

for(int i = 0; i < id; i++)
{
if(i == 0)
printf("%d (%d)", b[i], a[b[i]]);
else
printf(" * %d (%d)", b[i], a[b[i]]);
}
}
int main(void)
{
int T, cas;

init();

scanf("%d", &T);

cas = 0;

while(T--)
{
cas++;

scanf("%d", &n);

input(n);
printf("Case %d: ", cas);
deal();
printf("\n");
}
return 0;
}

最新文章

  1. mysql相关文章
  2. 【BZOJ1251】序列终结者 Splay
  3. AngularJS的小知识点
  4. crontab 每月最后一天
  5. js为表格添加行和列
  6. 20160329javaweb之JSP -session入门
  7. Serif和Sans-serif字体的区别(转)
  8. java常用用代码
  9. csharp中DateTime总结
  10. liunx命令3
  11. Zookeeper的安装的配置
  12. Python和Java的硬盘夜话
  13. MyBatis源码解析(三)——Transaction事务模块
  14. Java实现对象的序列化
  15. Python记录6:函数2,函数参数
  16. 做好平衡有多难?谈MMO的职业设计
  17. [设计模式]适配器模式Adapter
  18. 分享:将WDCP中的PHP5.2 1.7升级到PHP 5.3的方法
  19. 超简单将Centos的yum源更换为国内的阿里云源
  20. hdu4009最小树形图

热门文章

  1. Linux查看端口监听占用
  2. spring boot 的 userRepository无法注入的问题
  3. [apue] 使用 Ctrl+S停止输出而不用挂起前台进程
  4. 【STACK】Several待填的坑
  5. 把本地仓库同步到github上去
  6. JZOJ4238 纪念碑
  7. python笔记05
  8. 面向初学者的指南:创建时间序列预测 (使用Python)
  9. atx测试框架实现手机应用UI自动化测试
  10. CF572_Div2_D2