1. 程式人生 > >hdu5528(積性函式+尤拉函式)

hdu5528(積性函式+尤拉函式)

題意:設f(m)=\sum_{a=0}^{m-1}\sum_{b=0}^{m-1}[ab\,mod\,\,m>0](題目已把f(6)的表給出),g(n)=\sum_{m|n}f(m),給定n(n<=1e9),求g(n)

最主要的是求f(m),考慮到模數大於0的情況比較多,所以考慮考慮求ij mod n==0的情況。。

然後其實從題目給的表中看出對一個a來說,他0的個數和gcd(a,n)有關,其實也很容易證明,對一個數a來說,取完gcd部分,其餘部分都是多餘的,所以只要考慮gcd的部分產生的影響就行,然後就差n/gcd這些因子,即只要b取的是n/gcd的倍數,那麼就可以使ab為n的倍數,那麼這些b共有n/(n/gcd)=gcd個,所以對每個a,他產生ab餘數為0的個數為gcd(a,n),即

f(m)\\=m^2-\sum_{a=0}^{m-1}gcd(a,m) \\=m^2-\sum_{a=1}^{m}gcd(a,m) \\=m^2-\sum_{d=1}^{m}\sum_{i=1}^{\frac{m}{d}}d[gcd(i,\frac{m}{d})=1] \\=m^2-\sum_{d=1}^{m}d\varphi(\frac{m}{d})

然後

g(n)\\=\sum_{m|n}(m^2-\sum_{d=1}^{m}d\varphi(\frac{m}{d})) \\=\sigma_2(n)-\sum_{m|n}\sum_{d=1}^{m}\frac{m}{d}\varphi(d) \\=\sigma_2(n)-\sum_{d}\sigma_1(\frac{n}{d})\varphi(d)

\sigma_2(n)為積性函式,可以通過素因子分解求出

h(n)=\sum_{d}\sigma_1(\frac{n}{d})\varphi(d),由於h(n)符合狄利克雷卷積形式,故h(n)也為積性函式

h(p^k)\\=\sum_{i=0}^{k}\sigma_1(p^{k-i})\varphi(p^i) \\=\sum_{i=1}^{k}\frac{1-p^{k-i+1}}{1-p}*(p-1)p^{i-1}+\sum_{i=0}^{k}p^i \\=\sum_{i=1}^{k}(p^k-p^{i-1})+\sum_{i=0}^{k}p^i \\=(k+1)p^k

因此h(n)也可通過素因子分解求出。。

複雜度為O(Tsqrt(n))

然後又被卡常qaq

分解的時候可以列舉sqrt(n)以內的素因子分解,實測會快10倍左右。。

 

 

 

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神獸保佑,程式碼無bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll unsigned long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define NM 200015
#define nm 200005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+7;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}











int n,tot,prime[NM],m;
ll _ans,ans;
bool v[NM];

void init(){
    m=40000;
    inc(i,2,m){
    if(!v[i])prime[++tot]=i;
    inc(j,1,tot){
        if(i*prime[j]>m)break;
        v[i*prime[j]]++;
        if(i%prime[j]==0)break;
    }
    }
}

int main(){
    init();
    int _=read();while(_--){
    scanf("%d",&n);_ans=ans=1;
    for(int i=1;sqr(prime[i])<=n&&i<=tot;i++)if(n%prime[i]==0){
        int t=1;ll s=1,k=1;
        while(n%prime[i]==0)n/=prime[i],t++,s*=prime[i],k+=sqr(s);
        _ans*=t*s;ans*=k;
    }
    if(n>1)_ans*=2*n,ans*=1ll*n*n+1;
    ans-=_ans;
    printf("%llu\n",ans);
    }
    return 0;
}

 

 

 

Count a * b

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1367    Accepted Submission(s): 464


 

Problem Description

Marry likes to count the number of ways to choose two non-negative integers a

and b less than m to make a×b mod m≠0.

Let's denote f(m) as the number of ways to choose two non-negative integers a and b less than m to make a×b mod m≠0.

She has calculated a lot of f(m) for different m, and now she is interested in another function g(n)=∑m|nf(m). For example, g(6)=f(1)+f(2)+f(3)+f(6)=0+1+4+21=26. She needs you to double check the answer.



Give you n. Your task is to find g(n) modulo 264.

 

 

Input

The first line contains an integer T indicating the total number of test cases. Each test case is a line with a positive integer n.

1≤T≤20000
1≤n≤109

 

 

Output

For each test case, print one integer s, representing g(n) modulo 264.

 

 

Sample Input

 

2 6 514

 

 

Sample Output

 

26 328194

 

 

Source

2015ACM/ICPC亞洲區長春站-重現賽(感謝東北師大)

 

 

Recommend

hujie   |   We have carefully selected several similar problems for you:  6447 6446 6445 6444 6443 

 

 

Statistic | Submit | Discuss | Note