hdu 5212 Code
2016-12-07 21:31
302 查看
题目链接
分析:这道题可以尝试容斥原理或者莫比乌斯反演,其实本质上是一样的。
处理一下每个素数在给定的条件中有多少个倍数,然后容斥原理或者莫比乌斯反演统计一下gcd(x,y)=t的(x,y)的对数,最后统计一下答案,取模。
代码:
分析:这道题可以尝试容斥原理或者莫比乌斯反演,其实本质上是一样的。
处理一下每个素数在给定的条件中有多少个倍数,然后容斥原理或者莫比乌斯反演统计一下gcd(x,y)=t的(x,y)的对数,最后统计一下答案,取模。
代码:
/*****************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define offcin ios::sync_with_stdio(false) #define DEBUG freopen("d.txt", "r", stdin) #define sigma_size 26 #define lson l,m,v<<1 #define rson m+1,r,v<<1|1 #define slch v<<1 #define srch v<<1|1 #define sgetmid int m = (l+r)>>1 #define ll long long #define ull unsigned long long #define lowbit(x) (x&-x) #define bits(a) __builtin_popcount(a) const int INF = 0x3f3f3f3f; const ll INFF = 1e18; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-9; const ll mod = 10007; const int maxmat = 10; const ull BASE = 133333331; /*****************************************************/ inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; } /*****************************************************/ const int maxn = 1e4 + 5; std::vector<int> prime; int u[maxn]; bool mark[maxn]; int cnt[maxn]; int N; void init() { memset(mark, false, sizeof(mark)); u[1] = 1; for (int i = 2; i <= 1e4; i ++) { if (!mark[i]) { prime.push_back(i); u[i] = -1; } for 4000 (unsigned j = 0; j < prime.size(); j ++) { int tmp = prime[j] * i; if (tmp > 1e4) break; mark[tmp] = true; if (i % prime[j] == 0) { u[tmp] = 0; break; } else u[tmp] = -u[i]; } } } void get_c(int x) { for (int i = 2; i * i <= x; i ++) { if (x % i == 0) { cnt[i] ++; if (i * i != x) cnt[x / i] ++; } } cnt[x] ++; } int main(int argc, char const *argv[]) { // DEBUG; init(); while (~scanf("%d", &N)) { memset(cnt, 0, sizeof(cnt)); ll ans = 0; int m = -1; for (int i = 0; i < N; i ++) { int x; scanf("%d", &x); ans += (ll)(x - 1) * x % mod; m = max(x, m); get_c(x); } for (int i = 2; i <= m; i ++) { ll num = 0; for (int j = i; j <= m; j += i) num += (ll)u[j / i] * cnt[j] * (cnt[j] - 1); ans = (ans + num * (ll)(i - 1) * i % mod) % mod; } printf("%I64d\n", ans); } return 0; }
相关文章推荐
- HTML5+CSS3+JS学习笔记-4
- 项目整体管理(一)
- LintCode:单例
- 离散数学及其应用学习笔记——主定理(Master Theorem)的证明
- react-native--万能Button封装
- 【Qt学习笔记】7.对话框Dialog
- SpringMVC学习笔记(四)
- 基于 SquashFS 构建 Linux 可读写文件系统
- 烂泥:python2.7和python3.5源码安装
- Caffe学习笔记3
- 判断GPS、网络是否开启
- bzoj 1433: [ZJOI2009]假期的宿舍 网络流
- SpringMVC学习系列(5) 之 数据绑定-2
- Android Studio 2.2.3 发布
- 关于HBase的概述
- Caffe学习笔记2
- Caffe 学习笔记1
- Python profiling
- LeetCode-Array-26 Remove Duplicates from Sorted Array
- Mac电脑连接widows servers 2012 服务器