HDU 5382 GCD?LCM!

11 篇文章 0 订阅
7 篇文章 0 订阅

Description

First we define:
(1) lcm(a,b) , the least common multiple of two integers a and b, is the smallest positive integer that is divisible by both a and b. for example, lcm(2,3)=6 and lcm(4,6)=12 .
(2) gcd(a,b) , the greatest common divisor of two integers a and b, is the largest positive integer that divides both a and b without a remainder, gcd(2,3)=1 and gcd(4,6)=2 .
(3) [exp] , exp is a logical expression, if the result of exp is true , then [exp]=1 , else [exp]=0 . for example, [1+23]=1 and [1+24]=0 .

Now Stilwell wants to calculate such a problem:

F(n)=i=1nj=1n [ lcm(i,j)+gcd(i,j)n ]S(n)=i=1nF(i)

Find S(n) mod 258280327 .

Solution

这题是非常有借鉴意义的,它揭露了数论里一个很重要的思想:把大问题化小再通过迭代搞没掉[一般用于这种大于等于的题目](记得ZJOI2016Day1 orzrzz再讲数论的时候某PE题就是用这样的思路,只是那时啥都不懂)

先带着把问题化小的思路进入题目。
观察到 n<=1,000,000 ,说明可以线性递推

F(n)=i=1nj=1n[gcd(i,j)+lcm(i,j)n]

=i=1nj=1n[gcd(i,j)+lcm(i,j)n1]  i=1nj=1n[gcd(i,j)+lcm(i,j)=n]

=F(n1)+n21+i=1nj=1n[gcd(i,j)+lcm(i,j)=n1]

定义

T(n)=i=1nj=1n[gcd(i,j)+lcm(i,j)=n]

=>F(n)=F(n1)+n21T(n1)

化解 T(n)
T(n)=i=1nj=1n[gcd(i,j)+lcm(i,j)=n]

=i=1nj=1n[gcd(i,j)+ij/gcd(i,j)=n]

=di=1floor(n/d)j=1floor(n/d)[(ij+1)d=n]

=di=1floor(n/d)j=1floor(n/d)[gcd(i,j)=1][(ij+1)d=n]

=d|ni=1floor(n/d)j=1floor(n/d)[gcd(i,j)=1][ij=n/d1]................................(1)

定义 G(n)=d|n[gcd(d,n/d)=1]
(1) =G(n/d1)

G(n) 是积性函数,推导如下:
n 的某因子次数为αi αi>1 ,则 d 中要不包含αi p ,要不不包含p
所以对于 p 为素数,(x,p),若 gcd(x,p)=1 ,则 G(xp)=G(x)G(p)
否则 G(xp)=G(x)

不如Eular筛一发得到 G 的值
然后枚举d,枚举 n/d ,可以 O(nln(n)) 的得到 T
然后由推导式线性得到F
然后前缀和得到 S <script type="math/tex" id="MathJax-Element-24156">S</script>
然后结束了

Code

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vec;
typedef priority_queue<int> pq;

#define pb push_back
#define ph push
#define fi first
#define se second

inline void Max(int &a,int b){if(a<b)a=b;}
inline void Min(int &a,int b){if(a>b||a==-1)a=b;}
template<class T>void rd(T &a){
    a=0;char c;
    while(c=getchar(),!isdigit(c));
    do a=a*10+(c^48);
        while(c=getchar(),isdigit(c));
}
template<class T>void nt(T x){
    if(!x)return;
    nt(x/10);
    putchar(48+x%10);
}
template<class T>void pt(T x){
    if(!x)putchar('0');
    else nt(x);
}
const int M=1e6+5;
const int P=258280327;
int prime[M],g[M],T[M],f[M],s[M],_,t;
bool mark[M];
inline void Mod_add(int &a,int b){if((a+=b)>=P)a-=P;}
inline void Eular(){
    g[1]=1;
    for(int i=2;i<M;++i){
        if(!mark[i])g[i]=2,prime[t++]=i;
        for(int j=0,p;j<t&&1ll*(p=prime[j])*i<M;++j){
            mark[i*p]=1;
            if(i%p==0){g[i*p]=g[i];break;}
            g[p*i]=g[i]<<1;
        }
    }
    for(int d=1;d<M;++d)
        for(int k=1;d*k<M;++k)
            Mod_add(T[d*k],g[k-1]);
    for(int i=1;i<M;++i)
        f[i]=((f[i-1]+(i<<1)-1-T[i-1])%P+P)%P;
    for(int i=1;i<M;++i)
        s[i]=(s[i-1]+f[i])%P;
}
inline void gao(){
    int n;cin>>n;
    pt(s[n]),putchar('\n');
}
int main(){
    for(cin>>_,Eular();_--;)gao();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值