您的位置:首页 > 其它

RXD and math(莫比乌斯函数,快速幂)

2017-08-07 17:04 218 查看
RXD is a good mathematician.

One day he wants to calculate:

∑i=1nkμ2(i)×⌊nki−−−√⌋

output the answer module 109+7.
1≤n,k≤1018

μ(n)=1(n=1)

μ(n)=(−1)k(n=p1p2…pk)

μ(n)=0(otherwise)

p1,p2,p3…pk are
different prime numbers

 

Input
There are several test cases, please keep reading until EOF.

There are exact 10000 cases.

For each test case, there are 2 numbers n,k.
 

Output
For each test case, output "Case #x: y", which means the test case number and the answer.
 

Sample Input

10 10

 

Sample Output

Case #1: 999999937

这道题的特点是数据范围大,输入数量少,应该首先想到打表找规律。简单打表即可发现,答案就是n^k
注意到一个数字xx必然会被唯一表示成a^2\times
ba​2​​×b的形式.其中|\mu(b)|
= 1∣μ(b)∣=1。
所以这个式子会把[1,
n^k][1,n​k​​]的每个整数恰好算一次.
所以答案就是n^kn​k​​,快速幂即可.
时间复杂度O(\log
k)O(logk).
把后面 ⌊nki−−−√⌋这个数看做b,在【1,n^k】中
最多有⌊nki−−−√⌋个数使得前面那个u(i)不等于0,所以一乘法就是n^k/i,接下来【1,n^k】相加就是n^k 




n

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
long long quickmod(long long a,long long b,long long m)
{
a=a%mod,b%=mod-1;   //利用费马小定理a^b%c=a^(b%(c-1))%c
long long ans = 1;
while(b)//用一个循环从右到左便利b的所有二进制位
{
if(b&1)//判断此时b[i]的二进制位是否为1
{
ans = (ans*a)%m;//乘到结果上,这里a是a^(2^i)%m
b--;//把该为变0
}
b/=2;
a = a*a%m;
}
return ans;
}
int main()
{
ll x,y,ca=1;
while(~scanf("%I64d%I64d",&x,&y))
{
printf("Case #%I64d: %I64d\n",ca++,quickmod(x,y,mod));
}
}


快速幂模板
long long quickmod(long long a,long long b,long long m)
{
//x=x%mod,y%=mod-1;   //利用费马小定理a^b%c=a^(b%(c-1))%c
long long ans = 1;
while(b)//用一个循环从右到左便利b的所有二进制位
{
if(b&1)//判断此时b[i]的二进制位是否为1
{
ans = (ans*a)%m;//乘到结果上,这里a是a^(2^i)%m
}
b>>=1;
a = a*a%m;
}
return ans;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: