Leading and Trailing

题目

题意:求n^k的前3位和后3位。 

后3位用快速幂求;求前3位需要知道:一个数n,它可以写成10^x,其中这个x为浮点数,则n^k=(10^x)^k=10^x*k=(10^A)*(10^B);其中A,B分别是x*k的整数部分和小数部分。n^k 它的位数由(10^A)决定,它的位数上的值则有(10^B)决定,因此我们要求n^k的前三位,只需要将10^B求出,再乘以100,就得到了它的前三位。

将A和B分离可以用到一个函数modf():   B = modf(k*log10(n),&A);

modf()是分解x,以得到x的整数和小数部分。

double modf(double x,double *integer)

返回x的小数部分,符号与x相同。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
ll qpow(ll n,ll k,int mod)
{
    ll ans = 1,base = n;
    while(k)
    {
        if(k&1)
            ans = ans * base%mod;
            base=base*base%mod;
          k = k >>1;
    }
    return ans%mod;
}
int main()
{
    int T,n,k;cin>>T;
    double A,B;
    int ans1,ans2;
    int Case=1;
    while(T--)
    {
        cin>>n>>k;
         ans2 = qpow(n,k,1000);
         B = modf(k*log10(n),&A);
         ans1 = pow(10,B)*100;
        printf("Case %d: %d %03d\n",Case++,ans1,ans2);
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值