hdu 5528 Count a * b


题目描述:

Count a * b

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

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

题解:

经典的数论的题目.比赛时能做出来真是队友给力.
说一下最终的推导方法吧:(公式编辑太麻烦啦..我公式就不写了…http://async.icpc-camp.org/d/203-changchun-2015-b-count-a-times-b 可以看这个链接写的公式)
首先要写出表达式:两部分,前面是m^2, 后面是一个gcd的求和式.两部分前面套了一个枚举约数.
前面的不用看(可以暴力枚举约数,也可以利用积性函数的性质更快一点算).
后面的显然要换序:先只看里面的换序,因为gcd(i,m)和第一层第二层都有关,并且一般都会枚举gcd是谁.
之后可以再换一次序,当然也可以发现是积性的就去推公式.
也可以发现一个公式,然后化简到最简.
最后一步暴力枚举约数. 暴力枚举约数的时候也有一个比较快的小技巧.根据约数排序去除.

听昂神讲的时候学到积性函数可以o(n)的对1e6以内打表 质因数分解复杂度的求1e9的数.
然后推公式的时候gcd(a,b)==k 可变成 gcd(a/k,b/k)==1,写成gcd(m,n)==1, 可变成莫比乌斯反演.然后提到最前面

重点:

数论推公式+换序+积性

代码:
//没利用积性的性质.
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;

const int N = 1e5;

ull ans;
int n;
int p[N], tot;
int xp[N], num[N], xpn;
int b[N], bn;
bool flag[N];

int Euler() {
    int tmp;
    tot = 0;
    memset(flag, 0, sizeof(flag));
    for(int i = 2; i < N; ++i) {
        if(!flag[i]) {
            p[tot++] = i;
        }
        for(int j = 0; j < tot && 1ll * i * p[j] < N; ++j) {
            tmp = i * p[j];
            flag[tmp] = true;
            if(i % p[j]) {
                ;
            }
            else {
                break;
            }
        }
    }
}

void yue(int x)
{
    int xx = x;
    xpn = 0;
    ans = 0;
    for(int i = 0;i<tot&&p[i]*p[i]<=x;i++)
    {
        if(x%p[i]==0)
        {
            xp[xpn] = p[i];
            num[xpn] = 0;
            while(x%p[i]==0)
            {
                num[xpn]++;
                x /= p[i];
            }
            xpn++;
            if(x==1)
                break;
        }
    }
    if(x!=1)
    {
        xp[xpn] = x;
        num[xpn] = 1;
        xpn++;
    }
    b[0] = xx;
    bn = 1;
    for(int i = 0;i<xpn;i++)
    {
        int tmpbn = bn;
        int tmp = 1;
        for(int j = 1;j<=num[i];j++)
        {
            tmp *= xp[i];
            for(int k = 0;k<tmpbn;k++)
            {
                b[bn] = b[k]/tmp;
                bn++;
            }

        }
    }

    for (int i = 0;i < bn;i++)
    {
        ull bx = b[i];
        ull nn = n;
        ans += bx * bx;
        ans -= nn;
    }
    printf("%I64u\n",ans);
}

int main() {
 //   freopen("in.txt","r",stdin);
    int T;
    scanf("%d", &T);
    Euler();
    while (T--) {
        scanf("%d",&n);
        yue(n);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值