【HDU 5528】Count a * b(推导)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coldfresh/article/details/82189233

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 | n f ( 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 2 64 .
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 10 9

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亚洲区长春站-重现赛(感谢东北师大)

先推一下 f :

f ( m ) = m 2 i = 1 m j = 1 m m | i j = m 2 i = 1 m j = 1 m m ( m , i ) | i ( m , i ) j

可以发现 m ( m , i ) i ( m , i ) 已经是互质了,所以要整除m, j 必须是 m ( m , i ) 的倍数,其数量显然是 ( m , i ) 个,则:
f ( m ) = m 2 i = 1 m ( m , i )

这里是老套路,去枚举gcd:最终就有:
f ( m ) = m 2 d | m d ϕ ( m d )

来看 g ,所以:

g ( n ) = m | n m 2 m | n d | m d ϕ ( m d )

把d拿到最外层:
g ( n ) = m | n m 2 d | n d d | m , m | n ϕ ( m d ) = g ( n ) = m | n m 2 d | n d k | n d ϕ ( k ) = g ( n ) = m | n m 2 d | n n

到这里就是去枚举n的所以因子了,用唯一分解来做,然后搜索枚举因子即可。
代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ull  unsigned long long
#define ll long long
#define ul unsigned int
#define maxn 40000
using namespace std;
bool isP[maxn];
int prime[maxn];
int cnt;
void init()
{
    for(int i=2;i<maxn;i++)
    {
        if(!isP[i])prime[cnt++]=i;
        for(int j=0;j<cnt&&i*prime[j]<maxn;j++)
        {
            isP[i*prime[j]]=true;
            if(i%prime[j]==0)break;
        }
    }
}
int fac[50];
int e[50];
int k;
void getFac(int x)
{
    k=0;
    for(int i=0;prime[i]*prime[i]<=x;i++)
    {
        if(x%prime[i]==0)
        {
            fac[k]=prime[i];
            e[k]=0;
            while(x%prime[i]==0)
            {
                x/=prime[i];
                ++e[k];
            }
            ++k;
        }
    }
    if(x>1){fac[k]=x;e[k++]=1;}
}
ull ans=0;
void dfs(int cur,ull temp)
{
    if(cur==k)
    {
        ans+=temp*temp;
        return;
    }
    dfs(cur+1,temp);
    ull now=temp;
    for(int i=1;i<=e[cur];i++)
    {
        now*=fac[cur];
        dfs(cur+1,now);
    }
}
ull mod=0;
int main()
{
    mod=~mod;
    init();
    int t;
    cin>>t;
    int n;
    while(t--)
    {
        scanf("%d",&n);
        int total=1;
        getFac(n);
        ans=0;
        dfs(0,1);
        for(int i=0;i<k;i++)total*=e[i]+1;
        ull _n=n;
        _n*=total;
        _n=mod-_n+1;
        cout<<ans+_n<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Coldfresh/article/details/82189233