当前位置: 代码迷 >> 综合 >> LightOJ-1035
  详细解决方案

LightOJ-1035

热度:57   发布时间:2024-01-31 12:12:42.0

数论带师wz很擅长数论,这天他开始研究阶乘了,但是他觉得阶乘的定义太麻烦了,因此他想重新定义阶乘的表达式,即

N! = p1 (p1的指数) * p2 (p2的指数) * …

,pi为素数,但是wz觉得这太简单了,不想做,所以拜托机智的你来帮助他,他会给你一个整数 N, 你需要将 N! (N的阶乘)转换为素数分解的形式.
Input
输入一个数 T (≤ 125), 表示样例的数量。

每组样例输入一个数 N (2 ≤ N ≤ 100)。

Output
对于每种情况,请按照示例中给出的以下格式打印案例编号和阶乘分解。

Case x: N = p1 (p1的指数) * p2 (p2的指数) * …

其中 x 是样例编号, p1, p2 … 是升序的素数。

Sample Input
3
2
3
6
Sample Output

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

分析:数据不大,打不打素数表都没问题,直接从2~n遍历,对每一个数的进行素因子分解,统计每一个素因子的幂数就可以了。
注意输出格式!!!注意输出格式!!!注意输出格式!
行与行之间没有空行!!!

#include <iostream>
#include <cstdio>
#include <string>
#include<cstring>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;int main()
{int t;cin>>t;int cas=0;int ss=t;while (t--){int n;cas++;scanf("%d",&n);int res[1000]={0};for(int i=n;i>=2;i--){int k=i;for(int j=2;j*j<=k;j++){while (k%j==0){res[j]++;k/=j;}}if(k!=1)res[k]+=1;}int s;for(int i=100;i>=2;i--)if(res[i]!=0){s=i;break;}printf("Case %d:",cas);printf(" %d = ",n);for(int i=2;i<=100;i++){if(res[i]!=0){printf("%d (%d)",i,res[i]);if(s!=i)printf(" * ");}}printf("\n");}return 0;
}