POJ1365 - Prime Land(质因数分解)
题目大意
给定一个数的质因子表达式,要求你计算机它的值,并减一,再对这个值进行质因数分解,输出表达式
题解
预处理一下,线性筛法筛下素数,然后求出值来之后再用筛选出的素数去分解。。。。其实主要就是字符串处理。。。
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#define MAXN 10000
char str[MAXN],s[MAXN][7];
int prime[MAXN*5],cnt=0;
bool check[MAXN*5];
void get_prime()
{
int high=MAXN*5;
memset(check,false,true);
for(int i=2; i<=high; i++)
{
if(!check[i])
prime[cnt++]=i;
for(int j=0; j<cnt&&i*prime[j]<=high; j++)
{
check[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
int get_sum(char *s)
{
int sum=0,len=strlen(s);
for(int i=0;i<len;i++)
sum=sum*10+s[i]-'0';
return sum;
}
int main()
{
char *p;
int ans;
get_prime();
while(gets(str))
{
bool first=false;
if(str[0]=='0') break;
int len=0;
p=strtok(str," ");
while (p!=NULL)
{
strcpy(s[len++],p);
p=strtok(NULL," ");
}
ans=1;
for(int i=0; i<len; i+=2)
{
int a,b;
a=get_sum(s[i]);
b=get_sum(s[i+1]);
ans*=(int)(pow(a*1.0,b*1.0));
}
ans--;
for(int i=cnt-1;i>=0&&ans!=1;i--)
{
if(ans%prime[i]==0)
{
int p=0;
while(ans%prime[i]==0)
{
p++;
ans/=prime[i];
}
if(!first)
{
printf("%d %d",prime[i],p);
first=true;
}
else
printf(" %d %d",prime[i],p);
}
}
printf("\n");
}
return 0;
}
POJ1365 - Prime Land(质因数分解)的更多相关文章
- [暑假集训--数论]poj1365 Prime Land
Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...
- 数学--数论--POJ1365——Prime Land
Description Everybody in the Prime Land is using a prime base number system. In this system, each po ...
- POJ1365 Prime Land【质因数分解】【素数】【水题】
题目链接: http://poj.org/problem?id=1365 题目大意: 告诉你一个数的质因数x的全部底数pi和幂ei.输出x-1的质因数的全部底数和幂 解题思路: 这道题不难.可是题意特 ...
- POJ1365:质因数分解
Prime Land Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3590 Accepted: 1623 Descri ...
- 2012_p1 质因数分解 (prime.cpp/c/pas)
2012_p1 质因数分解 (prime.cpp/c/pas) 时间限制: 1 Sec 内存限制: 128 MB提交: 80 解决: 27[提交][状态][讨论版][命题人:外部导入] 题目描述 ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- LightOJ 1356 Prime Independence(质因数分解+最大独立集+Hopcroft-Carp)
http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1356 题意: 给出n个数,问最多能选几个数,使得该集合中的 ...
- POJ 1365 Prime Land(数论)
题目链接: 传送门 Prime Land Time Limit: 1000MS Memory Limit: 10000K Description Everybody in the Prime ...
- 【BZOJ-4514】数字配对 最大费用最大流 + 质因数分解 + 二分图 + 贪心 + 线性筛
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 726 Solved: 309[Submit][Status ...
随机推荐
- NGUI系列教程一
NGUI是Unity的一个插件,使用它来制作你的游戏UI必定将事半功倍.为什么这么说呢?首先我们说说GUI与NGUI的区别,GUI是Unity自带的绘制界面工具,它的成像原理是基于表层的,所以执行效率 ...
- JS 操作URL(重要)
我们可以用javascript获得其中的各个部分1, window.location.href全部URl字符串(在浏览器中就是完整的地址栏)本例返回值: http://www.x2y2.com:80/ ...
- sqlserver分页;mysql分页;orcale分页 的sql 查询语句
一,sqlserver分页: )) ID FROM 表名 AS 表名1_1 ORDER BY ID)) ORDER BY ID; 二:mysql分页 )*每页显示的数据条数,当前页码 * 每页显示的数 ...
- hadoop-streaming 配置之---参数分割
map: -D stream.map.output.field.separator=. 定义mapoutput字段的分隔符为. 用户可以自定义分隔符(除了默认的tab) -D stream.num.m ...
- oracle srvctl 命令
SRVCTL命令可以控制RAC数据库中的instance,listener以及services. 通常SRVCTL在ORACLE用户下执行.下面我们来介绍srvctl命令. 1.通过SRVCTL命令来 ...
- 第九章 Mass Storage设备
9.1 Mass Storage设备介绍 USB的Mass Storage类是USB大容量储存设备类(Mass Storage Device Class).专门用于大容量存储设备,比如U盘.移动硬盘. ...
- leetcode面试准备: Game of Life
leetcode面试准备: Game of Life 1 题目 According to the Wikipedia's article: "The Game of Life, also k ...
- Python解决codeforces ---- 1
第一题 1A A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input stan ...
- Drawable(3)Color State List Resource
注意 Color State List Resource 与 Color不同,前者是颜色状态表.根据不同状态显示不同颜色,它是State list,里面有多种颜色,后者只是一个颜色. Color St ...
- 【HDOJ】1022 Train Problem I
栈和队列训练题目. #include <stdio.h> #include <string.h> #define MAXNUM 1005 char in[MAXNUM]; ch ...