LightOJ 1035 - Intelligent Factorial Factorization (求因子)
2016-02-05 23:37
411 查看
1035 - Intelligent Factorial Factorization
Given an integer N, you have to prime factorize N!(factorial
N).
Each case contains an integer N (2 ≤ N ≤ 100).
Case x: N = p1 (power of p1) * p2(power of p2) * ...
Here x is the case number, p1, p2... are primes in ascending order.
Case.3:.6.=.2.(4).*.3.(2).*.5.(1)
题意:分解n!成样例的输出样式
思路:暴力写写就好。。
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1001000
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int ans[110];
int main()
{
int t,n,i,j;
int cas=0;
scanf("%d",&t);
while(t--)
{
mem(ans);
scanf("%d",&n);
for(i=2;i<=n;i++)
{
int x=i;
for(j=2;j<=x;j++)
{
if(x%j==0)
{
int cnt=0;
while(x%j==0)
{
cnt++;
x/=j;
}
ans[j]+=cnt;
}
if(x==1)
break;
}
}
printf("Case %d: %d = ",++cas,n);
printf("2 (%d)",ans[2]);
for(i=3;i<=100;i++)
if(ans[i])
printf(" * %d (%d)",i,ans[i]);
printf("\n");
}
return 0;
}
PDF (English) | Statistics | Forum |
Time Limit: 0.5 second(s) | Memory Limit: 32 MB |
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 factorizationof 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 replacespace with '.') isCase.3:.6.=.2.(4).*.3.(2).*.5.(1)
题意:分解n!成样例的输出样式
思路:暴力写写就好。。
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1001000
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
int ans[110];
int main()
{
int t,n,i,j;
int cas=0;
scanf("%d",&t);
while(t--)
{
mem(ans);
scanf("%d",&n);
for(i=2;i<=n;i++)
{
int x=i;
for(j=2;j<=x;j++)
{
if(x%j==0)
{
int cnt=0;
while(x%j==0)
{
cnt++;
x/=j;
}
ans[j]+=cnt;
}
if(x==1)
break;
}
}
printf("Case %d: %d = ",++cas,n);
printf("2 (%d)",ans[2]);
for(i=3;i<=100;i++)
if(ans[i])
printf(" * %d (%d)",i,ans[i]);
printf("\n");
}
return 0;
}
相关文章推荐
- 跳跃表实现的原理
- 日元负利率和美元暴跌,是对人民币的夹击
- 8 种常用的 NoSQL 数据库系统对比分析
- Android与PC间进行Socket通信
- Mybatis typeAliases别名
- LeetCode 73. Set Matrix Zeroes
- 利用github搭建个人小站
- php判断ip是否在指定ip区间内
- Linux 文件I/O — 文件描述符详解
- jQuery8(常见方法next.prev等,常见方法练习)
- 做得刚好
- Mybatis 实现手机管理系统的持久化数据访问层
- 2015年笔记总结——技术篇
- Ubuntu中NetworkManager
- Linux服务器权限管理实践——添加用户只访问某些文件目录
- 【slighttpd】基于lighttpd架构的Server项目实战(7)—http-parser
- 我的2015我的Android
- Java面向对象知识点精华
- How To FIX VLC Player Not Opening On Kali Linux (Google Chrome同理)
- NoSQL开篇之为什么要使用NoSQL