题目链接 题意 : 定义不能被平方数整除的数为 Square-free Number 定义 F(i) = 有几对不同的 a 和 b 使得 i = a * b 且 a .b 都是 Square-free 给出一个 N 求 分析 : 首先 Square-free 有一个性质 就是用唯一分解定理将 Square-free Number 分解后 素因数的指数都是 1 那么对于 a.b 是 Square-free Number 相乘 a * b 得出的 i 其不会有素因子的指数超过 2 然后你要熟悉欧拉筛…
题目来源:https://nanti.jisuanke.com/t/A1956 题意:找一个数拆成无平方因子的组合数,然后求前缀和. 解题思路:我们可以把某个数分解质因数,如果某个数可以分解出三个相同的质数那么该f(n)=0,比如8=2*2*2,  24=2*2*2*3,所以f(8)=f(24)=0:如果该数是素数那么f(n)=2:并且我们可以发现,如果m,n互质,那么f(n*m)=f(n)*f(m): #include<iostream> #include<cstring> #i…
小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题,今天,小明想考考你对素数的认识.  问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为"美素数",如29,本身是素数,而且2+9 = 11也是素数,所以它是美素数.  给定一个区间,你能计算出这个区间内有多少个美素数吗? Input 第一行输入一个正整数T,表示总共有T组数据(T <= 10000).  接下来共T行,每行输入两个整数L,R(1<= L <= R <= 100…
欧拉函数: 对正整数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; }…
欧拉线性筛. 对于它的复杂度的计算大概思考了很久. procedure build_prime; var i,j:longint; begin fillchar(vis,sizeof(vis),true); prime[]:=; to maxn do begin if vis[i] then begin inc(prime[]); prime[prime[]]:=i; end; to prime[] do begin if i*prime[j]>maxn then break; vis[i*pr…
刚刚学了一种新的素数筛选法,效率比原先的要高一些,据说当n趋近于无穷大时这个的时间复杂度趋近O(n).本人水平有限,无法证明. 这是道水题,贴代码出来重点是欧拉筛选法.我把原来普通的筛选法贴出来. //2013-11-07-22.30 //poj 2909 #include <stdio.h> #include <string.h> const int maxn = (1<<15)+5; bool vis[maxn]; int pr[3416]; int cnt = 1…
bzoj3944 题目描述 输入 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 输出 一共T行,每行两个用空格分隔的数ans1,ans2 样例输入 6 1 2 8 13 30 2333 样例输出 1 1 2 0 22 -2 58 -3 278 -3 1655470 2 bzoj4805 同上,不需要求mu 题解 杜教筛 公式推导: 这里有一个难点(其实也不能算难),就是由枚举d|i到枚举j≤⌊n/i⌋.此时可以看作下面语句的i是上面语句的i/d,…
Input 2 Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cases. 归律是2的n-1次方但是n太大就用到了欧拉降幂 和之前的…
HDU - 2824 题意: 求[a,b]间的欧拉函数和.这道题卡内存,只能开一个数组. 思路: ϕ(n) = n * (p-1)/p * ... 可利用线性筛法求出所有ϕ(n) . #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <iomanip> #include <cstdlib> #include…
[BZOJ3944]Sum Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1,ans2 Sample Input 6 1 2 8 13 30 2333 Sample Output 1 1 2 0 22 -2 58 -3 278 -3 1655470 2 题解: 当i等于1时就是答案,剩余的部分递归算下去就行了(先预处理出1000000以内的答案,其余的答案要用…
题意 链接 定义 $f(x)$ 为满足以下条件的有序二元组 $(a, b)$ 的方案数(即 $(a, b)$ 与 $(b, a)$ 被认为是不同的方案): $x= ab$ $a$ 和 $b$ 均无平方因子(即因子中没有除1之外的完全平方数) 求 $\displaystyle \sum_{i=1}^nf(i), 1 \leq n\leq 2 \times 10^7$. 分析 显然,$f(n)$ 是积性函数,考虑线性筛. 当 $x$为素数时, $f(x)=2$,即 $(1,x)$ 和 $(x,1)$…
题意 设 $f(n)$ 为 $n=ab$ 的方案数,其中 $a,b$ 为无平方因子数.求 $\displaystyle  \sum_{i=1}^nf(i)$,$n \leq 2e7$. 分析 显然,可发现 $f = \mu ^2 * \mu ^2$. 即 $\displaystyle f(n) = \sum_{d | n} \mu^2(d) \mu^2(\frac{n}{d})$ 那么可以这样化简目标式 $$\begin{aligned} & \sum_{i = 1}^{n} f(i) \\ =…
题目链接 给a,b,p.有b个a的幂 #include <iostream> using namespace std; typedef long long LL; const LL N = 1e6+50; LL phi[N], vis[N], prime[N], cnt; void Euler() { for(LL i = 2;i < N;++i) { if(!vis[i]) { prime[cnt++] = i; phi[i] = i-1; } for(int j = 0;j <…
题意 设 $f(n)$ 为 $n=ab$ 的方案数,其中 $a,b$ 为无平方因子数. 例如,$f(6)=4$,因为 $6 = 1 \times 6 = 2 \times 3 = 3 \times 2 = 6 \times 1$, $f(12)=2$,因为 $12 = 2 \times 6 = 6 \times 2$. 求 $\displaystyle  \sum_{i=1}^nf(i)$,$n \leq 2e7$,共有 $T$ 组询问($T \leq 20$). 分析: 首先,$f(n)$ 可…
1968: [Ahoi2005]COMMON 约数研究 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 2939  Solved: 2169 [Submit][Status][Discuss] Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input 3 Sample Output 5 题解 我们知道一个数x的约数个数…
J. Sum 26.87% 1000ms 512000K   A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integer…
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 ;…
嗯.... 埃氏筛和欧拉筛的思想都是相似的: 如果一个数是素数,那么它的所有倍数都不是素数.... 这里主要介绍一下欧拉筛的思路:(欧拉筛的复杂度大约在O(n)左右... 定义一个prime数组,这个数组被称为“素数表”,里面的数都为素数:然后用一个vis数组,如果一个数不是素数,则标记为1. 然后把i从2到n进行枚举,如果它没被访问过,则将其加入素数表中:然后for循环素数表,如果i % prime[j] == 0,则break即可, 因为prime[j]作为i的一个质因数,在某一种情况下,它…
筛素数 void shai() { no[1]=true;no[0]=true; for(int i=2;i<=r;i++) { if(!no[i]) p[++p[0]]=i; int j=1,t=i*p[1]; while(j<=p[0] && t<=r) { no[t]=true; if(i%p[j]==0) //每一个数字都有最小质因子.这里往后的数都会被筛过的,break break; t=i*p[++j]; } } } O(n)筛欧拉函数 void find()…
求素数 题目描述 求小于n的所有素数的数量. 输入 多组输入,输入整数n(n<1000000),以0结束. 输出 输出n以内所有素数的个数. 示例输入 10 0 示例输出 4 提示 以这道题目为例,要找出n以内的素数, n<=1000000. 为了节省时间,用素数筛 先把1000000以内的素数全部标记出来! 埃拉托斯特尼筛法,此素数筛核心算法代码: 这样跑完这个代码,是素数的会标记为0, 不是素数的标记为1.  数据处理完毕! int f[1000004]; int i, j; memset…
题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19736   Accepted: 7962 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with…
Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16927   Accepted: 6764 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b)…
题面 洛谷 Bzoj 题解 很容易想到$O(nk)$的树形$dp$吧,设$f[i]$表示处理完这$i$颗子树的最小花费,同时再设一个$mi[i]$表示$i$到根节点$1$路径上的距离最小值.于是有: $ f[i]=\sum min(f[son[i]], mi[son[i]]) $ 这样就有$40$分了. 考虑优化:这里可以用虚树来优化,先把所有点按照$DFS$序进行排序,然后将相邻两个点的$LCA$以及$1$号点加入进$LCA$,然后虚树就构好了,考虑欧拉序的特殊性质,所以再还原出欧拉序,在上面…
题意:给N个数,求对每个数ai都满足最小的phi[x]>=ai的x之和. 分析:先预处理出每个数的欧拉函数值phi[x].对于每个数ai对应的最小x值,既可以二分逼近求出,也可以预处理打表求. #include<bits/stdc++.h> using namespace std; typedef long long LL; ; int phi[maxn]; int res[maxn]; bool isprime[maxn]; void Euler(){ //欧拉函数筛 ;i<ma…
前两天总结了素数筛法,其中就有Eular筛法.现在他又来了→→ φ(n),一般被称为欧拉函数.其定义为:小于n的正整数中与n互质的数的个数. 毕竟是伟大的数学家,所以以他名字命名的东西很多辣. 对于φ(n),我们有这样[三个性质]: (1) [若n为素数],则φ(n) = n - 1 显然,由于n为素数,1~n-1与n都只有公因子1,因此φ(n) = n - 1. 比如φ(11)=10={1,2,3,4,5,6,7,8,9,10}; (2) [若n = p^k],p为素数(即n为单个素数的整数幂…
欧拉函数&欧拉定理&降幂 总结 标签:数学方法--数论 阅读体验:https://zybuluo.com/Junlier/note/1300214 这年头不总结一下是真的容易忘,老了老了,要AFO了... 欧拉函数 介绍 欧拉函数写做\(\varphi[x]\),表示\(0\)到\(x\)中与\(x\)互质的数的个数 那么我们会有引理(对于素数\(p\)): \[\left\{ \begin{aligned} \varphi[p]=p-1\ --------------①\\ \varph…
线性筛的思想:每个被筛的数是通过它最小的质因子所筛去的. 这种思想保证了每个数只会被筛一次,从而达到线性.并且,这个思想实现起来非常巧妙(见代码注释)! 因为线性筛的操作中用到了倍数的关系去实现,因此欧拉函数可以顺便也计算出来,根据完全积性函数的性质还有数学推算,直接一条语句就算出来了. #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<a…
Given the value of N, you will have to find the value of G. The definition of G is given below:Here GCD(i, j) means the greatest common divisor of integer i and integer j.For those who have trouble understanding summation notation, the meaning of G i…
求 $\sum_{i=1}^{n}\sum_{j=1}^{n}ijgcd(i,j)$   考虑欧拉反演: $\sum_{d|n}\varphi(d)=n$   $\Rightarrow \sum_{i=1}^{n}\sum_{j=1}^{n}ij\sum_{d|gcd(i,j)}\varphi(d)$   $\Rightarrow \sum_{i=1}^{n}\sum_{j=1}^{n}ij\sum_{d|i,d|j}\varphi(d)$   $\Rightarrow \sum_{d=1}^{…
A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integers could be decomposed into prod…