POJ2909&&POJ1730基础数论

Goldbach's Conjecture
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9986 Accepted: 5885

Description

For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that

n = p1 + p2

This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. There can be many such numbers. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.

Input

An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 215. The end of the input is indicated by a number 0.

Output

Each output line should contain an integer number. No other characters should appear in the output.

Sample Input

6
10
12
0

Sample Output

1
2
1

给你一个偶数N,问你可以写成多少种两个奇数且这两个奇数都为质数相加的形式,直接素数打表一重循环搜过去。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

bool is_prime[33000];
int prime[33000];
int n;
const int maxn=33000;
void dabiao()
{
    memset(is_prime,0,sizeof(is_prime));
    is_prime[0]=is_prime[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if(is_prime[i]==1)
        {
            continue;
        }
        for(int j=2*i;j<maxn;j+=i)
        {
            is_prime[j]=1;
        }
    }
    int k=0;
    for(int i=3;i<maxn;i++)
    {
        if(is_prime[i]==0)
        {
            prime[k++]=i;
        }
    }

}
int main()
{
    dabiao();
    while(cin>>n)
    {
        if(n==0)
            return 0;
        int cnt=0;
        for(int i=3;i<=n/2;i+=2)
        {
            if(is_prime[i]==0&&is_prime[n-i]==0)
            {
                 cnt++;
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}
Language:
Perfect Pth Powers
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16491 Accepted: 3740

Description

We say that x is a perfect square if, for some integer b, x = b 2. Similarly, x is a perfect cube if, for some integer b, x = b 3. More generally, x is a perfect pth power if, for some integer b, x = b p. Given an integer x you are to determine the largest p such that x is a perfect p th power.

Input

Each test case is given by a line of input containing x. The value of x will have magnitude at least 2 and be within the range of a (32-bit) int in C, C++, and Java. A line containing 0 follows the last test case.

Output

For each test case, output a line giving the largest integer p such that x is a perfect p th power.

Sample Input

17
1073741824
25
0

Sample Output

1
30
2
题意:给你一个数N,问N最多可以最大可以写成X的多少次方,这个X不限制范围。
大坑所在:N可能为负数,那么最后的P次幂的P必须为奇数。
思路:素数打表+质因子分解,用欧拉公式可以知道N=a1^p1*a2^p2...an^pn,接着求P1---PN的最大公约数,就是所求的答案了。
eg:5625=3^2*5^4=3^2*5^2*5^2=(15)^2*5^2=75^2因此5625最多可以写成75^2,而非5625^1.

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

bool is_prime[100005];
int prime[100005];
int mi[100005];
const int maxn=66666;
void dabiao()
{
    memset(is_prime,0,sizeof(is_prime));
    is_prime[0]=is_prime[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if(is_prime[i]==1)
        {
            continue;
        }
        for(int j=2*i;j<maxn;j+=i)
        {
            is_prime[j]=1;
        }
    }
    int k=0;
    for(int i=2;i<maxn;i++)
    {
        if(is_prime[i]==0)
        {
            prime[k++]=i;
        }
    }
}

int gcd(int a,int b)
{
    if(b==0)
        return a;
    else return gcd(b,a%b);
}

int cal(int n)
{
    int cnt=0,num;
    int i=0;
    int x=n;
    while(n!=1)
    {
        num=0;
        if(abs(prime[i])>sqrt(fabs((double)n)))//不要算的情况
            break;
        if(n%prime[i]==0)
        {
            while(n%prime[i]==0)
            {
                num++;
                n/=prime[i];
            }
            mi[cnt]=num;//求每个对应的幂指数
            cnt++;//可以分解成多少个幂指数相乘的形式
        }
        i++;
    }
    if(cnt==0)
    {
        return 1;
    }
    if(x<0)//对负数就进行处理,因为答案必须是奇数
        for(int i=0;i<cnt;i++)
        while(mi[i]%2==0)
        mi[i]/=2;
    if(cnt==1)
        return mi[0];
    int tmp=gcd(mi[0],mi[1]);
    for(int i=2;i<cnt;i++)
        tmp=gcd(mi[i],tmp);
    return tmp;

}

int main()
{
    dabiao();
    int n,ans;
    while(cin>>n)
    {
        if(n==0)
            return 0;
        memset(mi,0,sizeof(mi));
        ans=cal(n);
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值