HITOJ Prime Land 1069 (求质因子) 好题
2015-12-10 14:16
225 查看
Prime Land
Source : ACM ICPC Central European Regional 1997 | |||
Time limit : 1 sec | Memory limit : 32 M |
Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0 denote the increasing sequence of all prime numbers. We know that x > 1 can be represented in only one way in
the form of product of powers of prime factors. This implies that there is an integer kx and uniquely determined integers ekx, ekx-1, ..., e1, e0, (ekx > 0), that x = p(ekx,kx)*p(ekx-1,kx-1)*...*p(e1,1)*p(e0,0). The sequence
(ekx, ekx-1, ... ,e1, e0)
is considered to be the representation of x in prime base number system.
It is really true that all numerical calculations in prime base number system can seem to us a little bit unusual, or even hard. In fact, the children in Prime Land learn to add to subtract numbers several years. On the other hand, multiplication and division
is very simple.
Recently, somebody has returned from a holiday in the Computer Land where small smart things called computers have been used. It has turned out that they could be used to make addition and subtraction in prime base number system much easier. It has been
decided to make an experiment and let a computer to do the operation ``minus one''.
Help people in the Prime Land and write a corresponding program.
For practical reasons we will write here the prime base representation as a sequence of such pi and ei from the prime base representation above for which ei > 0. We will keep decreasing order with regard to pi.
Input
The input consists of lines (at least one) each of which except the last contains prime base representation of just one positive integer greater than 2 and less or equal 32767. All numbers in the line are separated by one space. The last line contains number
0.
Output
The output contains one line for each but the last line of the input. If x is a positive integer contained in a line of the input, the line in the output will contain x - 1 in prime base representation. All numbers in the line are separated by one space. There
is no line in the output corresponding to the last ``null'' line of the input.
Sample Input
17 1 5 1 2 1 509 1 59 1 0
Sample Output
2 4 3 2 13 1 11 1 7 1 5 1 3 1 2 1
//题意:输入是一行数字,并且数字是两两成对的,第一个表示的是质因子,第二个表示的是这个质因子的次数。通过这些可以得到一个确定的值为n,现在想让你计算(n-1)的质因子,按照输入的方式将其表示出来。//思路;直接模拟
#include<stdio.h> #include<string.h> #include<algorithm> #define N 1000010 using namespace std; struct zz { int p; int e; }q[110]; bool pp[210]; int p[210]; int getp() { int i,j,k=0; for(i=2;i<=200;i++) { if(!pp[i]) { p[k++]=i; for(j=i*i;j<=200;j+=i) pp[j]=true; } } } void solve(int n) { int i,num=0; for(i=0;p[i]*p[i]<=n;i++) { if(n%p[i]==0) { n/=p[i]; q[num].p=p[i]; q[num].e=1; while(n%p[i]==0) { n/=p[i]; q[num].e=q[num].e+1; } num++; } if(n==1) break; } if(n>1) { q[num].e=1; q[num].p=n; num++; } for(i=num-1;i>0;i--) printf("%d %d ",q[i].p,q[i].e); printf("%d %d\n",q[0].p,q[0].e); } int main() { getp(); int n,a,b,i,j,k,m; char c; while(scanf("%d",&n),n) { k=0; q[k].p=n; scanf("%d",&a); q[k].e=a; k++;m=1; while(1) { scanf("%c",&c); if(c=='\n') break; scanf("%d %d",&a,&b); q[k].p=a; q[k].e=b; k++; } for(i=0;i<k;i++) { for(j=1;j<=q[i].e;j++) m*=q[i].p; } solve(m-1); } return 0; }
相关文章推荐
- 推荐C++ Primer 第五版
- 【可持久化线段树】poj 2104 静态区间第k大
- td高度不随内容变化display:block;display:block;display:block;display:block;display:block;
- Android_(自定义view_3)
- SignalR2 永久连接简单实例1
- pandas 对每一列数据进行标准化
- R中的SQL包--sqldf
- iframe替代方案
- 使用ant编译项目技能
- android如何在一个布局文件中包含另外一个布局文件(include属性)
- Swift的String类型转换如何实现?
- 单例模式
- 关于kettle插入报错的数据到目标表
- Adding and displaying a background
- instruments分析工具
- 将截断字符串或二进制数据
- C#如何调用SQLServer写的函数
- IOS-UIScrollView
- js操作数组函数实例小结
- MarkdownPad初使用