标签:style class blog code com get
题意: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; }
Acdreamoj1115(数学思维题),布布扣,bubuko.com
标签:style class blog code com get
原文地址:http://blog.csdn.net/xiefubao/article/details/30370833