HDU 6063 RXD and math (快速幂)
Description
给出n和k,计算这个表达式的值
\[\sum_{i=1}^{n^k}{{\mu(i)}^2 \times \left \lfloor \sqrt{\frac{n^k}{i}} \right \rfloor}
\]
\(1 \leqslant n,k \leqslant 10^8\)
其中\(\mu(n)\)是莫比乌斯函数,定义为
\[\mu(n)= \begin{cases}1,&n=1 \\ (-1)^k,&n=\prod_{n=1}^{k}{p_i}\ (p_i\ is\ primer) \\ 0, &otherwise \end{cases}
\]
\(1 \leqslant n,k \leqslant 10^{18}\),结果模\(10^9+7\)。
Input
多组用例,每组用例给出\(n\)和\(k\)。
Output
每组用例输出"Case #x: y",其中x表示用例组数,y表示答案。
Sample Input
10 10
Sample Output
Case #1: 999999937
Solution
这道题的特点是数据范围大,输入数量少,应该首先想到打表找规律。简单打表即可发现,答案就是\(n^k\)。
下面简要推导如何得出这个答案。每个数\(x\)可以唯一地表示成\(x=a^2 \cdot b\),\(|\mu(b)=1|\)的形式,其中\(b\)称为无平方因子数。对于每个\(b\),在\([1,n^k]\)中与它搭配的\(a\)最多有\(\lfloor \sqrt{\frac{n^k}{i}} \rfloor\)个。枚举每个\(b\),求和,这样\([1,n^k]\)中每个数都被统计了一次,答案就是\(n^k\)。
Code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 1e3 + 10;
ll power(ll a, ll b, ll mod)
{
ll ans = 1;
a %= mod; b %= mod - 1;
while (b)
{
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
int main()
{
ll n, k; int cas = 0;
while (~scanf("%lld%lld", &n, &k))
printf("Case #%d: %lld\n", ++cas, power(n, k, mod));
return 0;
}