ACdream1115 (数学题+stl枚举)

题目链接:http://acdream.info/problem?pid=1115

题目定义了“完美的数”,初始的完美的数是1,3;对于任意完美的数 a,b  有2+a*b+2*a+2*b也是完美的数

例如 a=1 ,b=1  ; 2+1*1+2*1+2*1=7  7也是完美的数;

每组样例输入一个数,判断其是否为完美的数;

数据范围 1-1e9;

Salmon And Cat

Time Limit: 2000/1000MS (Java/Others)
Memory Limit: 128000/64000KB (Java/Others)

Problem Description

Math is very important, for those who are also in school, make sure you will learn more about math.

Salmon and Cat are good friends.Today Salmon ask Cat to help her judge whether a number is perfect or not. Perfect number is a kind of number defined by like this.

First, 1 and 3 are perfect number.Then if a and b are perfect numbers, 2+ab+2a+2b is also a perfect number.For example, 1 and 1 are perfect numbers,  so 2+1+2+2 = 7 is perfect number.

If Cat can‘t help Salmon,  Salmon will be sad and Cat will be much more sad. So Cat must solve the problem to maintain their friendship. Can you help Cat to solve the problem?

Input

This problem contains multiple test cases.

Each test case contains one line.

Each line contains an interger n, 1 ≤ n ≤ 109.

Output

For each test case, if n is a perfect number, output “Yes”, otherwise output “No”.

Sample Input

3
7
8

Sample Output

Yes
Yes
No

小伙伴想出的方法,也就是标准答案:

对于任意的完美数C

c+2 =2+a*b+2*a+2*b+2=(a+2)*(b+2);  (公式啊~~看到数据跟这个式子就该想着推公式啊

又因为初始的完美数 是 1,3;

所以任意完美数(C+2)都将是(1+2)3的倍数或者(3+2)5的倍数;

于是有了这个ac代码:

#include <stdio.h>
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        n += 2;
        while(n%5==0) n/=5;
        while(n%3==0) n/=3;
        if(n == 1) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

下面是笔者的ac代码,刚学习stl,疯狂的运用啊

#include <stdio.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
priority_queue<ll, vector<ll> ,greater<ll > >pq;//构造优先队列,使最小的优先弹出
ll num[300];
ll num2[300];
set <ll> se;//set记录数据,使得重复的数不入队;
int binsearch(int n)//二分查找
{
    int head=0,tail=136;
    while(head<=tail)
    {
        if(num2[head]==n)
            return 1;
        if(num2[tail]==n)
            return 1;
        else
        {
            int mid=(head+tail)/2;
            if(num2[mid]==n)
                return 1;
            else if(num2[mid]>n)
            {
                tail=mid-1;
            }
            else
            {
                head=mid+1;
            }
        }
    }
    return 0;
}
int main()
{
    se.insert(1);
    se.insert(3);
    num[1]=1;num[2]=3;
    pq.push(7);
    for(int i=3;;i++)
    {
        ll a=pq.top();
        if(a>1e9)break;//如果由最小值求出的完美数超过范围,break;
        pq.pop();
        if(a!=num[i-1])//不同的数的组合,可能求出相同的完美数,判断一下
            num[i]=a;
        else
            i--;
                for(int j=1;j<=i;j++)
                {
                    ll ans=2+a*num[j]+2*a+2*num[j];
                    if(se.count(ans))//set应用
                        continue;
                    if(ans>1e10)
                        break;
                        pq.push(ans);
                se.insert(ans);
                }
    }
    priority_queue<ll, vector<ll> ,greater<ll > >pq2;
    pq2.push(3);
       for(int i=115;;i++)//之前打印了上面队列求出的数组num,i在115停止了
    {
        ll a=pq2.top();
        if(a>1e9) break;
        pq2.pop();
        if(a!=num[i-1])
            num[i]=a;
        else
            i--;
                for(int j=1;j<=i;j++)
                {
                    ll ans=2+a*num[j]+2*a+2*num[j];
                     if(se.count(ans))
                        continue;
                    if(ans>1e10)
                        break;
                        pq2.push(ans);
                        se.insert(ans);
                }
    }
    sort(num,num+240);//排序
 int k=0;
 for(int i=1;i<300;i++)
 {
     if(num[i]!=0&&num[i]!=num[i-1])//将num中 重复数字和0去掉
        num2[k++]=num[i];
 }
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int sign;
        sign=binsearch(n);//二分查找
        if(sign)
            printf("Yes\n");
        else
            printf("No\n");
    }

    return 0;
}

这个代码还是不可取的,不过实在推不出公式的时候,按照自己的想法去敲也是可以ac的。我的同学还是一个小白的时候,没学搜索算法都能ac了搜索题,

所以条条大路通罗马啊~

时间: 2024-08-25 01:19:01

ACdream1115 (数学题+stl枚举)的相关文章

poj 2907 Collecting Beepers 邮递员问题暴力解法

题意: 给起点和n个点,求从起点出发经过这n个点每个点至少一次再回到起点的最短路. 分析: 类似邮递员问题,直接用STL枚举访问顺序暴力解决. 代码: //poj 2907 //sep9 #include <iostream> #include <algorithm> using namespace std; int x[16],y[16]; int d[16][16]; int a[16]; int n; int main() { int cases; scanf("%

50道hdu基础搜索总结(转)

Dfs: 大部分是直接递归枚举,即求满足约束条件下的解,虽不用剪枝,但也需要代码能力. 练习递归枚举的题目: 1241       Oil Deposits (dfs的连通块个数) 1016       Prime Ring Problem 1584       蜘蛛牌(简单dfs,简单的剪枝,还有人用DP做(???)) 1426       Sudoku Killer(练习递归的好题目 or Dancing links(???)) 2510       符号三角形(打表题,写写打表程序还是不错

搜索题推荐

(转自网络博客): POJ POJ 1376 – Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=1376 题意:略 解法:bfs,A*-. POJ 2688 – Cleaning Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=2688 题意:bfs后转换为tsp问题 解法:bfs+dp,bfs+dfs 相关:http://hi.baidu.com/zfy0701/blo

枚举所有排列-STL中的next_permutation

枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 #include<cstdio> #include<algorithm> using namespace std; int main() { int n,p[10]; scanf("%d",&n); for(int i=0;i<n;i++) scan

枚举排列的两种常见方法

1.递归枚举 1 #include<iostream> 2 using namespace std; 3 4 void print_permutation(int n, int *p, int cur) 5 { 6 if (cur == n) 7 { 8 for (int i = 0; i < n; i++) 9 cout << p[i]; 10 cout << endl; 11 } 12 else for (int i = 1; i <= n; i++)

【整理】最近做的几道数学题

数学题也是有意思 那么总结一下 T1.[Bzoj2751][HAOI2012]容易题 题意 有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些限制即A[i]不能取哪些值,我们定义一个数列的积为该数列所有元素的乘积,求出所有可能的数列的积的和. 题解 考虑一个式子(...)(...)(...),()内为一些数相加,根据乘法分配律,打开这个式子就是每次从每个括号中选一个元素,再把所有可能选择的结果加起来. 这件事情就是我们题目的设问,于是我们只要算出来每个位置sigma可能的选择

STL初探——第二级配置器 __default_alloc_template的学习心得

SGI STL 第二级配置器使用的是memory pool,即内存池,相比较于第一级空间配置器,第二级空间配置器多了许多限制,主要是为了防止申请小额区块过多而造成内存碎片.当然小额区块在配置时实际上是对空间配置器效率的一种伤害.另外,索求任何一块内存,都得需要一些额外内存来进行标记,虽然这些标记占内存很小很小,但蚂蚁多咬死象,小区块多了,这些小标记还是挺浪费内存的,但这也无法避免,毕竟系统需要靠这些小标记管理内存. SGI 第二级配置器的做法是,如果区块足够大,超过128bytes时,就移交第一

UVa 140 (枚举排列) Bandwidth

题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. 还有就是不明白,为什么19.20行注释哪错了?? 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 10; 5 6 int id[256], letter[maxn]; 7 char in[1000];

Day4:T3搜索 T4数学题排列组合

T3:搜索 很出名的题吧,费解的开关 同T2一样也是一题很考思考的 附上题解再解释吧: 对于每个状态,算法只需要枚举第一行改变哪些灯的状态,只要第一行的状态固定了,接下来的状态改变方法都是唯一的:每一行需要改变状态的位置都在上一行中不亮的灯的正下面,因为只有这样才能使上一行的灯全亮.我们枚举第一行的状态改变方法(共2^5种),对于每种方法都依次改变下面几行的状态使上面一行灯全亮.到最后一行我们需要判断是否最后一行也恰好全亮,并更新最小步数. 首先需要找到第一行的状态,怎么写?枚举深搜啊 找到了其