传送门:https://projecteuler.net/problem=517
题意还是很简洁的。
刚开始打了一个表,发现没有什么规律,最后仔细盯了一下式子,发现这是将用1和根号n填,直到大于n-根号n的方案数。
显然这个东西是一个组合数,考虑枚举填了多少个根号,算一下方案即可。单词复杂度是根号的,跑了大概0.4s。
//waz
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)((x).size()))
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long int64;
typedef unsigned int uint;
typedef unsigned long long uint64;
#define gi(x) ((x) = F())
#define gii(x, y) (gi(x), gi(y))
#define giii(x, y, z) (gii(x, y), gi(z))
int F()
{
char ch;
int x, a;
while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-');
if (ch == '-') ch = getchar(), a = -1;
else a = 1;
x = ch - '0';
while (ch = getchar(), ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + ch - '0';
return a * x;
}
const int mod = 1e9 + 7;
int inc(int a, int b) { a += b; return a >= mod ? a - mod : a; }
int dec(int a, int b) { a -= b; return a < 0 ? a + mod : a; }
const int N = 2e7;
int fac[N + 10], rfac[N + 10];
int fpow(int a, int x)
{
int ret = 1;
for (; x; x >>= 1)
{
if (x & 1) ret = 1LL * ret * a % mod;
a = 1LL * a * a % mod;
}
return ret;
}
int C(int n, int m)
{
if (m < 0 || n < m) return 0;
return 1LL * fac[n] * rfac[m] % mod * rfac[n - m] % mod;
}
int work(int n)
{
int ans = 0;
double t = sqrt(n);
for (int i = 0; i * t < n - t; ++i)
{
int c = ((n - t) - i * t);
ans = inc(ans, C(c + i, i));
}
for (int i = 0; i < n - t; ++i)
{
int c = ((n - t) - i) / t;
int j = ((n - t) - c * t);
//for (int k = i; k <= j; ++k) ans = inc(ans, C(c + k, c));
ans = inc(ans, dec(C(c + j + 1, c + 1), C(c + i, c + 1)));
//cerr << i << ", " << c << ", " << j << endl;
i = j;
}
return ans;
}
unordered_map<double, int> zz;
double tt;
int work_bf(double n)
{
if (n < tt) return 1;
if (zz[n]) return zz[n];
return zz[n] = inc(work_bf(n - 1), work_bf(n - tt));
}
int bf(int n)
{
tt = sqrt(n);
zz.clear();
return work_bf(n);
}
int main()
{
for (int i = fac[0] = 1; i <= N; ++i) fac[i] = 1LL * fac[i - 1] * i % mod;
rfac[N] = fpow(fac[N], mod - 2); for (int i = N; i; --i) rfac[i - 1] = 1LL * rfac[i] * i % mod;
int l, r;
//cin >> l >> r;
l = 1e7; r = 1e7 + 1e4;
int ans = 0, gg = 0;
for (int i = l + 1; i < r; ++i)
{
bool fg = 1;
for (int j = 2; j * j <= i; ++j)
if (i % j == 0) fg = 0;
if (fg) ans = inc(ans, work(i));
}
printf("%d\n", ans);
return 0;
}