当前位置:网站首页>【8.5】Code Source - 【GCD】【Sequence Median】【Maximum Number of Concatenated Edges】
【8.5】Code Source - 【GCD】【Sequence Median】【Maximum Number of Concatenated Edges】
2022-08-07 01:02:00 【ZhgDgE】
#871. GCD
题意:给定 1 ≤ a < m ≤ 1 0 10 1\leq a<m\leq 10^{10} 1≤a<m≤1010 ,计算 ∑ x = 0 m − 1 [ gcd ( a , m ) = gcd ( a + x , m ) ] \sum_{x=0}^{m-1}{[\gcd(a,m)=\gcd(a+x,m)]} ∑x=0m−1[gcd(a,m)=gcd(a+x,m)] .
题解:(Interval coprime) 代码源每日一题 Div1 GCD
思路:推一下式子:
容易知道 k = gcd ( a , m ) k=\gcd(a,m) k=gcd(a,m) 一定是 m m m 的因子.我们先计算出 a + x a+x a+x is within the range k k k The left and right boundaries of multiples of ,The multiple range is [ l , r ] = [ ⌈ a k ⌉ , ⌊ a + m − 1 k ⌋ ] [l,r]=[\left \lceil \frac a k \right \rceil,\left \lfloor \frac{a+m-1} k \right \rfloor] [l,r]=[⌈ka⌉,⌊ka+m−1⌋] .那么 ∑ x = 0 m − 1 [ gcd ( a , m ) = gcd ( a + x , m ) ] = ∑ i = l r [ gcd ( i , m k ) = 1 ] \sum_{x=0}^{m-1}{[\gcd(a,m)=\gcd(a+x,m)]}=\sum_{i=l}^r{[\gcd(i,\frac m k)=1]} ∑x=0m−1[gcd(a,m)=gcd(a+x,m)]=∑i=lr[gcd(i,km)=1] ,那么问题转化为求 l ∼ r l\sim r l∼r 中有多少个数与 m k \frac m k km 互质.
A classic problem is asking 1 ∼ n 1\sim n 1∼n 有多少个数与 m m m 互质,方法为:对 m m m 分解质因子,Then the binary enumeration is inclusive and exclusive,时间复杂度 O ( n + 2 11 ) O(\sqrt n+2^{11}) O(n+211) ,这里的 11 11 11 是指 1 0 10 10^{10} 1010 The number inside has the most 11 11 11 个质因子.
总结了一些关于 gcd \gcd gcd 的性质:
gcd ( a , b ) = gcd ( a , a + b ) \gcd(a,b)=\gcd(a,a+b) gcd(a,b)=gcd(a,a+b) ,The prefix of the sequence and the sequence or the difference sequence gcd \gcd gcd 相等
gcd ( k × a , k × b ) = k × gcd ( a , b ) \gcd(k\times a,k\times b)=k \times \gcd(a,b) gcd(k×a,k×b)=k×gcd(a,b)
gcd ( a , b ) = p × gcd ( a p , b p ) \gcd(a,b)=p\times \gcd(\frac a p,\frac b p) gcd(a,b)=p×gcd(pa,pb) ,要保证 p p p 是 a , b a,b a,b 的因子
gcd ( a k , b k ) = gcd k ( a , b ) \gcd(a^k,b^k)=\gcd^k(a,b) gcd(ak,bk)=gcdk(a,b)
c ∣ a × b ⇒ c gcd ( b , c ) ∣ a c|a\times b\Rightarrow \frac c{\gcd(b,c)}|a c∣a×b⇒gcd(b,c)c∣a
模板:
LL cal(LL n, LL m){
// numbers of i in 1 ~ n : gcd(i, m) == 1
vector<LL> vec;
for(LL i = 2; i * i <= m; i ++ ){
if(m % i == 0){
while(m % i == 0) m /= i;
vec.pb(i);
}
}
if(m > 1) vec.pb(m);
LL res = 0;
for(int bit = 1; bit < 1 << vec.size(); bit ++ ){
LL mul = 1;
rep(i, 0, (int)vec.size() - 1) if(bit >> i & 1) mul *= vec[i];
if(__builtin_popcount(bit) & 1) res += n / mul;
else res -= n / mul;
}
return n - res;
}
AC代码:http://oj.daimayuan.top/submission/321288
#877. Sequence median
题意:给定正整数 N N N , 求出 1 ∼ N − 1 1∼N−1 1∼N−1 中所有与 N N N A sequence of relatively prime numbers 的中位数.
我们定义 : 一个长度为 K K K The median of the sequence is the first in the sequence ⌊ K + 1 2 ⌋ ⌊\frac{K+1}2⌋ ⌊2K+1⌋ 大的数字. and two positive integers a a a 与 b b b Coprime if and only if gcd ( a , b ) = 1 \gcd(a,b)=1 gcd(a,b)=1 .
思路:Play the table to find the rules.证明的话: gcd ( i , x ) = gcd ( x − i , x ) \gcd(i,x)=\gcd(x-i,x) gcd(i,x)=gcd(x−i,x) ,Probably this is the formula.
AC代码:http://oj.daimayuan.top/submission/322050
#916. Maximum number of edges
题意: 1 ≤ n ≤ 1 0 5 1\leq n\leq 10^5 1≤n≤105

题解:(树状数组/DP) 代码源每日一题 Maximum number of edges
思路:树状数组优化 DP .
We enumerate by subscript a a a 里的元素,去枚举 b b b corresponding element.我们先定义 d p ( i , j ) dp(i,j) dp(i,j) 表示 a a a 前 i i i 个元素和 b b b 前 j j j Maximum match of elements,转移方程为 d p ( i , j ) = max 1 ≤ i ′ < i , 1 ≤ j ′ < j d p ( i ′ , j ′ ) + 1 dp(i,j)=\max_{1\leq i'<i,1\leq j'<j}{dp(i',j')+1} dp(i,j)=max1≤i′<i,1≤j′<jdp(i′,j′)+1 .
It can be found that this is a two-dimensional partial order,The first dimension can be optimized.for and currently enumerated i i i 对应的 j j j ,转移方程为 d p ( j ) = max ( d p ′ ( j ) , max k = 1 j − 1 d p ′ ( k ) + 1 ) dp(j)=\max(dp'(j),\max_{k=1}^{j-1}dp'(k)+1) dp(j)=max(dp′(j),maxk=1j−1dp′(k)+1) ,Just use the tree array to maintain the prefix value of the second dimension.
边栏推荐
猜你喜欢
随机推荐
语音识别与转换小试牛刀(1)
P4315 月下“毛景树”(树链剖分)
Redis(六) 主从模式
【8.5】代码源 - 【GCD】【序列中位数】【最大连边数量】
Introduction to Ftrace function graph
【Koltin Flow(三)】Flow操作符之中间操作符(三)
yum源修改
Distillation Learning Framework Cheat Sheet (1)
Open3D ROR滤波
禁用防火墙后,aria2的6800端口还是不通
连接 MySQL 报错:2059 - authentication plugin ‘caching_sha2_password‘ cannot be loaded...
time complexity and space complexity
工程仪器设备在线监测管理系统常见问题和注意事项
Contrastive Learning Model Cheat Sheet (1)
超分辨率模型小抄(1)
软件测试面试题:手工测试与自动测试有哪些区别?
OnePose: 无CAD模型的one-shot物体姿态估计(CVPR 2022)
2022年G2电站锅炉司炉题库及在线模拟考试
重磅!一起学习ORB-SLAM3技术
【华为云至简致远】还在烦恼成本高、运维难?华为云数据库给你一个标准答案!









