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
ba2×b的形式.其中|\mu(b)|
= 1∣μ(b)∣=1。
所以这个式子会把[1,
n^k][1,nk]的每个整数恰好算一次.
所以答案就是n^knk,快速幂即可.
时间复杂度O(\log
k)O(logk).
把后面 ⌊nki−−−√⌋这个数看做b,在【1,n^k】中
最多有⌊nki−−−√⌋个数使得前面那个u(i)不等于0,所以一乘法就是n^k/i,接下来【1,n^k】相加就是n^k
n
快速幂模板
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
ba2×b的形式.其中|\mu(b)|
= 1∣μ(b)∣=1。
所以这个式子会把[1,
n^k][1,nk]的每个整数恰好算一次.
所以答案就是n^knk,快速幂即可.
时间复杂度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; }
相关文章推荐
- HDU 6063 RXD and math+(快速幂)+多校联赛第三场
- HDU 6063 RXD and math【快速幂】【打表找规律】
- 2017杭电多校联赛第三场-RXD and math (hdu6063) 找规律快速幂
- (2017多校训练第三场)HDU - 6063 RXD and math 找规律 + 快速幂
- HDU6063 [2017多校联合3] RXD and math 打表 快速幂
- HDU6063 RXD and math 快速幂
- HDU 6063 RXD and math 打表找规律 快速幂
- 2017多校联合三/hdu6063 ( RXD and math )快速幂+思维
- HDU 6063 RXD and math(快速幂)
- hdu 6063 RXD and math(快速幂)
- hdoj 6063(2017 Multi-University Training Contest - Team 3) RXD and math
- HDU 6063 RXD and math
- RXD and math HDU - 6063
- HDU6063-RXD and math
- RXD and math
- RXD and math
- hdu6063 RXD and math
- 2017 多校3 1008 RXD and math
- HDU6063 2017杭电多校联赛第三场-RXD and math
- HDU 6063 RXD and math(数论)