Acdreamoj1115(数学思维题)

题意:1,3是完美数,如果a,b是完美数,则2+a*b+2*a+2*b,判断给出的n是否是完美数。


解法:开始只看出来2+a*b+2*a+2*b=(a+2)*(b+2)-2,没推出更多结论,囧。没办法,只能暴力将所有的完美数求出来然后查表。正解是c+2=(a+2)*(b+2);完美数都是有质因子3或5组成的(5本身除外);


自己暴力代码:

/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=1010;
const int INF=1000000000;

LL num[Max];
int help[Max];
LL get(int l,int r)
{
    return 2*(num[l]+1)*(num[r]+1)-num[l]*num[r];
}
struct point
{
    LL ans;
    int h;
};
bool operator<(const point& a,const point& b)
{
    return a.ans>b.ans;
}
priority_queue<point> pri;
int main()
{
    num[0]=1;
    num[1]=3;
    point p;
    p.ans=7;
    p.h=0;
    pri.push(p);
    p.ans=23;
    p.h=1;
    pri.push(p);
    for(int i=0; i<Max; i++)
        help[i]=i;
    help[0]=1;
    help[1]=2;
    int po=2;
    while(pri.top().ans<=INF)
    {
        point ptop=pri.top();
        pri.pop();
        if(num[po-1]==ptop.ans)
        {
            point p;
            p.ans=get(ptop.h,help[ptop.h]);
            p.h=ptop.h;
            pri.push(p);
            help[ptop.h]++;
            continue;
        }
        point p;
        p.ans=get(ptop.h,help[ptop.h]);
        p.h=ptop.h;
        pri.push(p);
        help[ptop.h]++;
       // cout<<ptop.ans<<" ";
        num[po++]=ptop.ans;

        p.ans=get(po-1,po-1);
        p.h=po-1;
        pri.push(p);
        help[po-1]=po;
    }
    int t;
    while(scanf("%d",&t)==1)
    {
        if(binary_search(num,num+po,t))
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值