hdu 6051 If the starlight never fade [欧拉函数] [2017 Multi-University Training Contest - Team 2]

本文介绍了一种解决特定数学问题的方法,该问题涉及到非负整数、素数及其组合条件下的数学计算。通过定义函数f(i),讨论了如何计算满足特定条件的(x, y)对的数量,并给出了求解过程及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If the starlight never fade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 104    Accepted Submission(s): 50


Problem Description
We will give you a non-negative integer m and a prime number p .
And we define f(i) is the number of pair (x,y) that satisfies (x+y)ixi%p and 1xp1,1ym .
Now, you have to calculate the sum p1i=1if(i) .
Maybe the sum is too big,so you only need to output the sum after mod 1e9+7 .
 

Input
The first line contains only one integer T , which indicates the number of test cases.
For each test case, there are a integer m(1mp1) and a prime number p(2p1e9+7) on one line.
 

Output
For each test case, output one line "Case #x: y", where x is the case number (starting from 1) and y is the sum after mod 1e9+7 .
 

Sample Input
  
  
3 5 7 3 11 2 103
 

Sample Output
  
  
Case #1: 210 Case #2: 390 Case #3: 50388
  
  
 

题解 : http://bestcoder.hdu.edu.cn/blog/


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<cmath>
typedef long long ll;
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
using namespace std;

ll phi(ll n) {
    if(n == 1) return 2;
    ll ans = n;
    for(int i = 2; i * i <= n; i++) {
        if(n % i == 0) {
            ans -= ans / i;
            while(n % i == 0) n /= i;
        }
        if(n == 1) break;
    }
    if(n > 1) ans -= ans / n;
    return ans;
}

int main() {
    int m, t, p, vi = 1;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &m, &p);
        vector<int> v;
        int t = sqrt(p - 0.5);
        for(int i = 1; i <= t; i++) {
            if((p - 1) % i == 0) {
                v.push_back(i);
                if((p - 1) / i != i) v.push_back((p - 1) / i);
            }
        }
        ll ans = 0;
        for(int i = 0; i < v.size(); i++) {
            //printf("%d ", v[i]);
            ll g = (p - 1) / v[i];
            g = g * phi(g) / 2;
            g %= mod;
            ans = (ans + ((ll)v[i] * v[i] % mod) * g) % mod;
        }
        //printf("\nans = %lld\n", ans);
        ans = ((ans - (ll)p * (p - 1) / 2) % mod + mod) % mod;
        ans = ans * m % mod;
        printf("Case #%d: %lld\n", vi++, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值