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;
}
HDU 2034 是一道经典的 A-B Problem 题目,通常涉及简单的数学运算或者字符串处理逻辑。以下是对此类问题的分析以及可能的解决方法。 ### HDU 2034 的题目概述 该题目要求计算两个数之间的差值 \(A - B\) 并输出结果。需要注意的是,输入数据可能存在多种情况,因此程序需要能够适应不同的边界条件和特殊情况[^1]。 #### 输入描述 - 多组测试数据。 - 每组测试数据包含两行,分别表示整数 \(A\) 和 \(B\)。 #### 输出描述 对于每组测试数据,输出一行表示 \(A - B\) 的结果。 --- ### 解决方案 此类问题的核心在于正确读取多组输入并执行减法操作。以下是实现此功能的一种常见方式: ```python while True: try: a = int(input()) b = int(input()) print(a - b) except EOFError: break ``` 上述代码片段通过循环不断接收输入直到遇到文件结束符 (EOF),适用于批量处理多组测试数据的情况。 --- ### 特殊考虑事项 尽管基本思路简单明了,在实际编码过程中仍需注意以下几点: 1. **大数值支持**:如果题目中的 \(A\) 或 \(B\) 可能非常大,则应选用可以容纳高精度的数据类型来存储这些变量。 2. **负数处理**:当 \(B>A\) 导致结果为负时,确保程序不会因符号错误而失效。 3. **异常捕获**:为了防止运行期间由于非法字符或其他意外状况引发崩溃,建议加入必要的错误检测机制。 --- ### 示例解释 假设给定如下样例输入: ``` 5 3 7 2 ``` 按照以上算法流程依次完成各步操作后得到的结果应当分别为 `2` 和 `5`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值