Project Eular 216 cid=1765#problem/F

\(t(n)=2n^2-1\)
\(2<=n<=5e7\)中,\(t(n)有多少个质数\)

  • \(2,3,5\)不整除\(t(n)\)
  • \(p|t(n)\),则\(p|t(kp+n),p|t(kp-n)\)\(k\)为任意整数
  • 对于当前数组元素\(a[i]\),若\(a[i]!=1\),则\(a[i]\)是素数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 50000010;
ll a[maxn];
int main()
{
    int n, ans = 0;
    scanf("%d", &n);
    for (ll i = 2; i <= n; i++)
    {
        a[i] = (2ll * i * i - 1);
    }
    for (ll i = 2; i <= n; i++)
    {
        if (a[i] == 2ll * i * i - 1)//若存在素因子,则在此之前已经被除过,所以会变小,即会不相等
            ans++;
        if (a[i] > 1)  //经过它之前的数的考验,仍然>1,一定是素数
        {
            ll p = a[i];
            for (ll j = i + p; j <= n; j += p)//类似埃筛
            {
                if (a[j] % p == 0)
                    a[j] /= p;
            }
            for (ll j = -i; j <= n; j += p)//上面第二点,两种情况不包含,分别进行操作
            {
                if (j > 0 && a[j] % p == 0)
                    a[j] /= p;
            }
        }
    }
    printf("%lld\n", ans);
    return 0;
}
posted @ 2020-06-29 20:27  Zeronera  阅读(92)  评论(0)    收藏  举报