light oj 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;
}
light oj 1035 - Intelligent Factorial Factorization 素因子分解的更多相关文章
- Intelligent Factorial Factorization LightOJ - 1035(水题)
就是暴力嘛...很水的一个题... 不好意思交都... #include <iostream> #include <cstdio> #include <sstream&g ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
- Jan's light oj 01--二分搜索篇
碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...
随机推荐
- python中各种文件打开模式
在python中,总的来说有三种大的模式打开文件,分别是:a, w, r 当以a模式打开时,只能写文件,而且是在文件末尾添加内容. 当以a+模式打开时,可以写文件,也可读文件,可是在读文件的时候,会发 ...
- .NET为什么要使用异步(async)编程?⭐⭐⭐⭐⭐
.NET为什么要使用异步(async)编程? 有几点坐下笔记 ⭐⭐⭐⭐: 1. 同步方法 static void Main(string[] args) { Console.WriteLine($&q ...
- Nginx的一理解(2)
1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置:
- html+css 知识点总结 day1(01-08)
01 初步认识浏览器 02 浏览器内核 IE 内核:Trident, win10 Edge 内核:EdgeHTML Firefox(火狐浏览器) 内核:Ge ...
- Nginx作为web静态资源服务器——跨域访问
跨站访问 为什么浏览器禁止跨域访问 Nginx跨站访问 Syntax:add_header name value [always]; Default:—— Context:http,serve ...
- ORM执行原生SQL语句
# 1.connectionfrom django.db import connection, connections cursor = connection.cursor() # cursor = ...
- java小心机(3)| 浅析finalize()
每天进步一丢丢,连接梦与想 如果你停止就是谷底,如果你还在努力就是上坡 系列文章 java"小心机"(1)[资源彩蛋!] java小心机(2)| 重载和构造器的小细节 预备知识 J ...
- .NET Core Install for Ubuntu 14.04
Add the dotnet apt-get feed In order to install .NET Core on Ubuntu or Linux Mint, you need to fir ...
- AMD R5 2400G插帧教程
最近买的小主机带的是AMD R5 2400G显卡,支持AMD的插帧技术,Sandeepin肯定要体验一把效果. BlueskyFRC 按照网上的教程配置,似乎2400G显卡驱动里没有AMD Fluid ...
- Oracle数据库、实例、用户、表空间、表之间的关系
完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等): 2) Oracle数据库实例则是一组Ora ...