POJ-1365-Prime Land
来源:互联网 发布:遗传算法优化svm参数 编辑:程序博客网 时间:2024/05/21 17:37
题目给出你一个数分解成几个素数相乘的形式,
例如样例2:
5 1 2 1
10=5^1*2^1
然后要求你求出根据所给展开形式得到原数num,求num-1的展开形式
思路:
我先用筛数法把素数全部找出来,然后依次尝试,若找到最后一个素数因子都还没办法分解完,那么剩下的数就为素数,直接加到最后。需要注意的是用long long 保存
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int maxn=10000;int pri[maxn],cnt;bool ispri[maxn];long long ansa[maxn],ansb[maxn],count;void Init(){ ispri[1]=1; cnt=0; for(int i=2;i<maxn;i++) {if(!ispri[i]) pri[cnt++]=i;for(int j=2;i*j<maxn;j++) ispri[i*j]=1; }}long long QuickMul(int a,int b){ long long ans=1; long long val=a; while(b) {if(b&1) ans*=val;val*=val;b>>=1; } return ans;}void GetAns(long long num){ count=0; for(int i=0;i<cnt&&num>1;i++) {if(num%pri[i]==0){ ansa[count]=pri[i]; ansb[count]=0; while(num>1&&num%pri[i]==0) {ansb[count]++;num/=pri[i]; } count++;} } if(num>1) {ansa[count]=num;ansb[count++]=1; } printf("%lld %lld",ansa[count-1],ansb[count-1]); count--; for(int i=count-1;i>=0;i--)printf(" %lld %lld",ansa[i],ansb[i]); printf("\n");}int main(){ Init(); char ch; while(scanf("%c",&ch)!=EOF) {if(ch=='0') break;int res=ch-'0',last=0;long long num=1;while(1){ scanf("%c",&ch); if(ch=='\n') {num*=QuickMul(last,res);break; } if(ch==' ') {if(!last){ last=res; res=0;}else{ num*=QuickMul(last,res); res=last=0;} } elseres=res*10+ch-'0';}GetAns(num-1); } return 0;}
- POJ 1365 Prime Land
- POJ-1365-Prime Land
- poj 1365 Prime Land
- POJ 1365 Prime Land
- POJ 1365 Prime Land
- POJ 1365 Prime Land
- POJ 1365 -- Prime Land
- POJ 1365 Prime Land
- POJ 1365 Prime Land 素数
- POJ-1365 Prime Land (数论)
- POJ 1365 Prime Land 整数分解
- poj 1365 Prime Land 【简单数论】
- poj 1365 Prime Land (质因子分解)
- POJ 1365 Prime Land(素因子分解)
- POJ-1365-Prime Land 解题报告
- [水+整数分解] poj 1365 Prime Land
- POJ 1365 Prime Land(分解质因数)
- poj 1365 Prime Land 质因数分解
- MySQL配置文件优化(Innodb)
- AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a
- 【Win8不好用?简单方法助您还原之前系统】
- hdu 1402 A * B Problem Plus
- UVA 11987 - Almost Union-Find
- POJ-1365-Prime Land
- 开源成为一种趋势,iMatrix平台强势归来!
- ArcGIS License Manager 静默授权命令摘录
- plsql-聚簇表初解
- libnl3安装及使用
- A Security-focused HTTP Primer---http://danielmiessler.com/popular/
- VC++无标题窗口以及控件拖动的方法(修正)
- 怎样学会理财?
- HDU--1251 -- 统计难题 [字典树基本应用] [用数组事先分配空间以节约时间]