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 ...
随机推荐
- Stop a hung service 关闭一个无响应的windows 服务
If you ever have trouble with a service being stuck in a 'starting' or 'stopping' state, you can run ...
- 我的PHP之旅--数据库连接MySQL服务器,添加 删除 查询
PHP连接MySQL服务器 连接MySQL的方法:mysql_connect(); 语法:resource $link = mysql_connect($hostname, $username, $p ...
- BZOJ 1596: [Usaco2008 Jan]电话网络
Description Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无 ...
- codeforces 390E Inna and Large Sweet Matrix
本题的主要算法就是区间更新和区间求和: 可以用线段树和树状数组来做: 感觉线段树写的太麻烦了,看到官方题解上说可以用树状数组做,觉得很神奇,以前用过的树状数组都是单点维护,区间求和的: 其实树状数组还 ...
- 总结: Struts2 + Spring 的线程安全问题
1. 首先Struts2 本身是安全的 其原理大概是:Strtus2会获取到用户的http请求,然后负责给每个请求实例化一个Action 对象,但是大家注意,这里的action对象和Struts1里面 ...
- jquery checkbox获取多个选项
http://www.jb51.net/article/27186.htm http://www.cnblogs.com/libingql/archive/2011/11/07/2238663.htm ...
- WCF - Architecture
WCF - Architecture WCF has a layered architecture that offers ample support for developing various d ...
- ☀【移动】UC极速模式
UC浏览器的部分版本默认是“极速”模式,有何办法能控制UC自动改变其浏览模式? √http://www.zhihu.com/question/20582949 关于UC极速模式下访问网站错乱 √htt ...
- 《C#并行编程高级教程》第4章 并发集合 笔记
这一章主要介绍了System.Collections.Concurrent下的几个类. ConcurrentQueue<T> 并发队列.完全无锁,使用CAS(compare-and-swa ...
- c#打印机设置,取得打印机列表及相应打印机的所有纸张格式
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...