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)
i
≡x
i
%p
and
1≤x≤p−1,1≤y≤m
.
Now, you have to calculate the sum ∑
p−1
i=1
if(i)
.
Maybe the sum is too big,so you only need to output the sum after mod 1e9+7
.
And we define f(i)
Now, you have to calculate the sum ∑
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(1≤m≤p−1)
and a prime number
p(2≤p≤1e9+7)
on one line.
For each test case, there are a integer m(1≤m≤p−1)
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;
}