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

来源:互联网 发布:淘宝导航二级下拉菜单 编辑:程序博客网 时间:2024/04/30 00:43

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 mod1e9+7.
 

Sample Input
35 73 112 103
 

Sample Output
Case #1: 210Case #2: 390Case #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;}


阅读全文
0 0
原创粉丝点击