线性筛的思想:每个被筛的数是通过它最小的质因子所筛去的. 这种思想保证了每个数只会被筛一次,从而达到线性.并且,这个思想实现起来非常巧妙(见代码注释)! 因为线性筛的操作中用到了倍数的关系去实现,因此欧拉函数可以顺便也计算出来,根据完全积性函数的性质还有数学推算,直接一条语句就算出来了. #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<a…
Description 给定n,m,求\(\sum_{i=1}^{n}\sum_{j=1}^{m}\varphi(ij)\)模10^9+7的值. Input 仅一行,两个整数n,m. Output 仅一行答案. Sample Input 100000 1000000000 Sample Output 857275582 数据规模 1<=n<=10^5,1<=m<=10^9. sol %%%ranwen!!! 前置技能: \(n=\sum_{d|n}\varphi(d)\) \(\v…
找了一些曾经没提到的算法.这应该是数学基础系最后一篇. 曾经的文章: 数学基础I 莫比乌斯反演I 莫比乌斯反演II 数学基础II 生成函数 数学基础III 博弈论 容斥原理(hidden) 线性基(hidden) 卡特兰数/第二类斯特林数(hidden) 置换群(hidden) 莫比乌斯反演III(hidden) 线性筛(hidden) 欧拉函数 计算单个欧拉函数 设\(n\)的唯一分解为\(p_i\),则\(\varphi(n)=n\prod(1-\frac{1}{p_i})\). 奇偶性 \…
一.题目 A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example, the p…
题目传送门 Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5434   Accepted: 3072 Description We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if the set { (xi mod p) | 1 <= i <= p-1 }…
2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss] Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 1<=N<=10^7 uva上做过gcd(x,y)=1的题 gcd(x,y)=p ---> gcd(x/p,y/p)=1 每个质数做一遍行了 答案是欧拉函数的前缀和*2…
O(n) 筛选素数 #include<bits/stdc++.h> using namespace std; const int M = 1e6 + 10 ; int mindiv[M] ;//每个数的最小质因数 int prim[M] , pnum ;//存素数 bool vis[M] ; void prim () { for (int i = 2 ; i < M ; i ++) { if (!vis[i]) { mindiv[i] = i ; prim[ pnum++ ] = i ;…
/* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体证明看po主的博客 ^0^ #超时:这里直接用欧拉函数暴力搞还是不可以的,用到线性筛欧拉函数,这里总和爆int,要用long long */ #include<bits/stdc++.h> #define ll long long using namespace std; /***********…
欧拉函数: 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. POJ 2407.Relatives-欧拉函数 代码O(sqrt(n)): ll euler(ll n){ ll ans=n; ;i*i<=n;i++){ //这里i*i只是为了减少运算次数,直接i<=n也没错, ){ //因为只有素因子才会加入公式运算.仔细想一下可以明白i*i的用意. ans=ans/i*(i-); ) n/=i; //去掉倍数 } } ) ans=ans/n*(n-); return ans; }…
题目描述 求 输入 第一行包含一个正整数T,表示有T组测试数据.接下来T<=10^5行,每行给出一个正整数N,N<=10^6. 输出 包含T行,依次给出对应的答案. 样例输入 7 1 10 100 1000 10000 100000 1000000 样例输出 1 2127 18446224 183011304660 1827127167830060 18269345553999897648 182690854273058293758232 题解 “高精度”+欧拉函数+线性筛 由于$i$和$j$…