SPOJ LCMSUM - LCM Sum】的更多相关文章

题意是求: $\sum_{i = 1}^{n}lcm(i, n)$ $= \sum_{i = 1}^{n}\frac{ni}{gcd(i, n)}$ $= n\sum_{i = 1}^{n}\frac{i}{gcd(i, n)}$ $= n\sum_{d|n}\sum_{i = 1}^{n}d*[gcd(i, n)==d]$ $= n\sum_{d|n}\sum_{i = 1}^{\frac{n}{d}}i*[gcd(i, \frac{n}{d})==1]$ $= n\sum_{d|n}\sum…
一个基于观察不依赖于反演的做法. 首先 \(\rm lcm\) 是不好算的,转化为计算 \(\rm gcd\) 的问题,求: \[\sum\limits_{i = 1} ^ n \frac{in}{\gcd(i, n)} \] 注意到 \(\gcd(n - i, n) = \gcd(i, n), (n - i) \times n + in = n ^ 2\),可以考虑将 \(\gcd(n - i, n), \gcd(i, n)\) 一起计算. 具体地,将原式乘 \(2\), 前后配对.需要注意的…
Problem code: LCMSUM Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n. Input The first line contains T the number of test cases. Each of the next T lines contain…
KPSUM - The Sum One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that he placed signs + and - between every pair of adjacent digits alternately. Now he wants to find the value of the expression he has made. Help him. For…
Special Thanks: Jane Alam Jan*At moment in University of Texas at San Antonio - USA You will be given n integers A1A2A3...An. Find a permutation of these n integers so that summation of the absolute differences between adjacent elements is maximized.…
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<<1,L,mid #define rson rt<<1|1,mid+1,R /* AC 水题 题意:给出n个数,两种操作 U i x 将第i个数改成x Q x y 查询[x,y]中两个数的和的最大值,这两个数不能为同一个 即只要求该区间第一大和第二大的数,他们的和即为答案 */ using nam…
给我们n个数,然后有m个询问,每个询问为L,R,询问区间[L,R]的最大最小值,最小公约数,最大公约数,和,异或,或,且 这些问题通通可以用RMQ的思想来解决. 以下用xor来作为例子 设dp[i][j]为以i开头的,长度为2^j的区间的所有值得异或 那么dp[i][j] = dp[i][j-1] xor dp[i+(1<<(j-1))][j-1] 这样,运用动态规划的思想,我们可以在nlogn的时间复杂度内算出以任意点开头的,长度为1,2,4,8...2^j 的区间的异或值. 那么询问任意区…
gcd套路变换 GCD https://www.luogu.org/problem/P2568 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. $ 1<=N<=10^7$ 答案是n*n矩阵中每两个数之间gcd==1的数对个数 考虑把\(n\times n\)的矩阵分成两部分,即从对角线劈开,设每一部分的答案为ans,则最终答案为$ ans*2 - 对角线上的(即n以内的质数个数)$ 这个把矩阵分成两部分的方法很有用,记一下吧 \[ ans = \…
P1890 gcd区间 \(\gcd\) 是满足结合律的,所以考虑用 ST 表解决 时间复杂度 \(O((n\log n+m)\log a_i)\) 考虑到 \(n\) 很小,你也可以直接算出所有的区间 \(\gcd\) 时间复杂度 \(O(n^2\log a_i+m)\) 实现的时候我写了 ST 表 加了 fread&fwrite IO优化,跑得还是非常快的 #include<stdio.h> #include<ctype.h> #define gc (l==r&…
初等数论学习笔记 I:同余相关. 初等数论学习笔记 II:分解质因数. 1. 数论函数 本篇笔记所有内容均与数论函数相关.因此充分了解各种数论函数的名称,定义,符号和性质是必要的. 1.1 相关定义 数论函数:定义域为正整数的函数称为 数论函数.因其在所有正整数处均有定义,故可视作数列.OI 中常见的数论函数的陪域(即可能的取值范围)为整数. 加性函数:若对于任意 \(a, b\in \mathbb{N}_+\) 且 \(a\perp b\) 均有 \(f(ab) = f(a) + f(b)\)…