博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Project Euler 521]Smallest prime factor 题解
阅读量:5025 次
发布时间:2019-06-12

本文共 3385 字,大约阅读时间需要 11 分钟。

为什么博客园的LaTex有问题啊。

 跑了40s左右,跑得还是有点久……

//waz#include 
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
PII;typedef vector
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;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 = 1e6 + 10;int64 n;int sump[N], sum2[N];int sqrtn;int p[N], m;namespace work1{ int sum(int64 x) { int64 y = x + 1; if (x & 1) y >>= 1; else x >>= 1; return (x % mod) * (y % mod) % mod; } int S(int64 x, int j) { if (x < sqrtn && x < 1LL * p[j + 1] * p[j + 1]) return sum2[x]; if (!j) { return dec(sum(x), 1); } if (1LL * p[j] * p[j] > x) return S(x, j - 1); else return dec(S(x, j - 1), 1LL * p[j] * dec(S(x / p[j], j - 1), sump[j - 1]) % mod); }}namespace work2{ const int K = 5000; unordered_map
ha; bitset<200010000> s; int dfs(int64 x, int i) { if (!i) return x % mod; if (!x) return 0; if (ha[i * n + x]) return ha[i * n + x]; int ans = x % mod; for (int j = i; j; --j) { ans = dec(ans, dfs(x / p[j], j - 1)); } return ha[i * n + x] = ans; } int solve() { int ans = 0; int k = 1; for (; p[k] < K && k < m; ++k); //cerr << k << endl; for (int i = 1; i <= k; ++i) ans = inc(ans, 1LL * p[i] * (dfs(n / p[i], i - 1) - 1) % mod); if (k == m) return ans; int64 ret = n / p[k] - 1; for (int i = 1; i <= k; ++i) for (int j = p[i], c = n / p[k]; j <= c; j += p[i]) if (!s[j]) s[j] = 1, --ret; for (int i = k + 1; i <= m; ++i) { int t = n / p[i - 1], tt = n / p[i]; for (int j = t; j > tt; --j) if (!s[j]) --ret; ans = inc(ans, ret % mod * p[i] % mod); for (int j = p[i]; j <= tt; j += p[i]) if (!s[j]) s[j] = 1, --ret; } return ans; }}main(){ cin >> n; sqrtn = sqrt(n); static bool fg[N]; for (int i = 2; i <= sqrtn; ++i) { if (!fg[i]) p[++m] = i; for (int j = 1; j <= m && p[j] * i <= sqrtn; ++j) { fg[p[j] * i] = 1; if (i % p[j] == 0) break; } } for (int i = 1; i <= m; ++i) sump[i] = inc(sump[i - 1], p[i]); for (int i = 2; i <= sqrtn; ++i) sum2[i] = inc(sum2[i - 1], i * (!fg[i])); int ans2 = work2::solve(); //cerr << "task2 : " << clock() << " ms" << endl; //cerr << ans2 << endl; int ans1 = work1::S(n, m); //cerr << "task1 : " << clock() << " ms" << endl; //cerr << ans1 << endl; cout << inc(ans1, ans2) << endl;}

 

转载于:https://www.cnblogs.com/AnzheWang/p/10447118.html

你可能感兴趣的文章
论文《A Generative Entity-Mention Model for Linking Entities with Knowledge Base》
查看>>
CentOS 6.7编译安装PHP 5.6
查看>>
Linux记录-salt分析
查看>>
Android Studio默认快捷键
查看>>
发布开源库到JCenter所遇到的一些问题记录
查看>>
第七周作业
查看>>
函数式编程与参数
查看>>
flush caches
查看>>
SSAS使用MDX生成脱机的多维数据集CUB文件
查看>>
ACM_hdu1102最小生成树练习
查看>>
MyBatis源码分析(一)--SqlSessionFactory的生成
查看>>
android中ListView点击和里边按钮或ImageView点击不能同时生效问题解决
查看>>
CTF常用工具之汇总
查看>>
java的面向对象 (2013-09-30-163写的日志迁移
查看>>
HDU 2191 【多重背包】
查看>>
51nod 1433 0和5【数论/九余定理】
查看>>
【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】
查看>>
less 分页显示文件内容
查看>>
如何对数据按某列进行分层处理
查看>>
[Qt] this application failed to start because it could not find or load the Qt platform plugin
查看>>