Uva 11029 Leading and Trailing (求n^k前3位和后3位)

题意:给你 n 和 k ,让你求 n^k 的前三位和后三位

思路:后三位很简单,直接快速幂就好,重点在于如何求前三位,注意前导0

资料:求n^k的前m位 博客连接地址

 

代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;

ll qmod(ll a,ll b,ll mod)
{
    ll ans=1;
    while(b)
    {
        if(b%2)
        {
            ans=(ans*a)%mod;
        }
        b=b/2;
        a=(a*a)%mod;
    }
    return ans;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
       ll n,k;
       scanf("%lld %lld",&n,&k);
       double a=k*log10((double)n);
       a=a-floor(a);
       double ans1 = pow(10,a)*100;
       ll ans2 = qmod(n,k,1000);
       cout<<(ll)ans1<<"...";
       printf("%03lld\n",ans2);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/simplekinght/p/6666449.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值