Leading and Trailing LightOJ - 1282

题意

You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk .
nk 的最高三位和最低三位

分析

1 最低三位,快速幂取模
2 最高三位 任取一个数 n ,有 t=lg(n),n=10t 其中 t=x+y ,x为t的整数部分
,y 为t的小数部分 ,x 是n的位数,因为 n = 10x n10x ,其中 y= lg n10x , 10y1000 就是数n的前三位

参考代码
int qpow(int n,int k,int m)//快速幂
{
    int ans = 1;
    LL nn = n;
    nn %= m;
    while(k>0)
    {
        if(k&1)
            ans =ans * nn % m;
        nn = nn * nn % m;
        k>>=1;
    }
    return ans;
}
int main(void)
{
    std::ios::sync_with_stdio(false);
    int T;
    cin>>T;
    int kase = 0;
    //  cout<<fmod(10.1,10);
    while(T--)
    {
        int n,k;
        cin>>n>>k;
        double tmp = (double)k*log10(1.0*n);
        tmp = fmod(tmp,1);
        tmp = pow(10,tmp);
    while(tmp<100)
        tmp *= 10;
    int ans1 = (int) tmp;
        int ans2 = qpow(n,k,1000);
        //cout<<"Case "<<++kase<<": "<<ans1<<" "<<ans2<<endl;
        printf("Case %d: %d %03d\n",++kase,ans1,ans2);


    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值