2017 ACM/ICPC Asia Regional Qingdao Online - 1011 A Cubic number and A Cubic Number

2017-09-17 17:12:11

writer:pprp

找规律,质数只有是两个相邻的立方数的差才能形成,公式就是3 * n * (n + 1) +1, 判断读入的数是不是满足

这次依然只是做了两道签到题,其他题做了,但是因为没有想到好的算法,所以没有通过,唯一值得高兴的是这两个都是我自己做的

 

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;
typedef long long ll;
const ll maxn = 1e12 + 1000;
ll x,y;

int main()
{
   ios::sync_with_stdio(false);
    freopen("in.txt","r",stdin);
    int cas;
    cin >> cas;
    while(cas--)
    {
        bool tag = 0;
        cin >> x;
        if((x - 1) % 3 == 0)
        {
            ll pprp = (x-1)/3;
            ll tmp = sqrt(pprp);
            for(ll i = 1 ; i <= tmp ; i++)
            {
                if(i * i + i == pprp)
                {
                    tag = 1;
                    break;
                }
            }
        }
        if(tag == 0)
            cout << "NO" << endl;
        else
            cout << "YES" << endl;

    }
    return 0;
}

 

posted @ 2017-09-17 17:15  pprp  阅读(170)  评论(0编辑  收藏  举报