HDU 6063 RXD and math (数论+快速幂)

RXD and math

                                                                Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
                                                                                      Total Submission(s): 1363    Accepted Submission(s): 747


Problem Description
RXD is a good mathematician.
One day he wants to calculate:
i=1nkμ2(i)×nki

output the answer module  109+7 .
1n,k1018
μ(n)=1(n=1)

μ(n)=(1)k(n=p1p2pk)

μ(n)=0(otherwise)

p1,p2,p3pk  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输出结果。

大言不惭的说数论,其实我是找到的规律。

当n=1,k=1时, ans=1
当n=2,k=1时, ans=2
当n=3,k=1时, ans=3
当n=1,k=2时, ans=1
当n=2,k=2时, ans=4
当n=3,k=2时, ans=9

然后,就不用再写下去了,ans不就是n^k吗?然后就用快速幂出结果就OK了,然而还是WA,这里的N和K应该可能很大,所以一开始就可以超过int,然后快速幂的第一步就爆了long long 所以,要先对n取模,再进行快速幂。

后来才知道那个U的函数叫做莫比乌斯函数,好像好多人都是找规律得到的答案。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}

int main()
{
	ll n,k,i,j,flag=1;
	while(~scanf("%lld%lld",&n,&k))
	{
		n=n%mod;
		ll ans=fpow(n,k);
		cout<<"Case #"<<flag++<<": "<<ans<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值