前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leading and Trailing(LightOJ - 1282)

Leading and Trailing(LightOJ - 1282)

作者头像
Lokinli
发布2023-03-09 16:29:44
1830
发布2023-03-09 16:29:44
举报
文章被收录于专栏:以终为始以终为始

题解:求一个数的次幂,然后输出前三位和后三位,后三位注意有前导0的情况。 后三位直接用快速幂取模求解。

          前三位求得时候只需要稍微变形一下,可以把乘过的结果拆成用科学计数法,那么小数部分只有由前面决定,所以取前三位利用double来计算就可以了。

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Mod = 1000;
ll ppow(ll a, ll k)  // 后三位
{
    ll ans = 1;
    while(k)
    {
        if(k%2)ans *= a;
        ans %= Mod;
        a *= a;
        a %= Mod;
        k /= 2;
    }
    return ans;
}
double Merge(double x)
{
    while(x >=1000.0)
    {
        x /= 10.0;
    }
    return x;
}
double dopow(double a, int k)  // 前三位
{
    double ans = 1.0;
    while(k)
    {
        if(k%2)ans *= a;
        ans = Merge(ans);
        a *= a;
        a = Merge(a);
        k /= 2;
    }
    return ans;
}
int main()
{
    int T,cas = 0;
    ll n, k;
    scanf("%d",&T);
    while(T--)
    {
        cas ++;
        scanf("%lld %lld", &n, &k);
        ll ans2 = ppow(n,k);
        double m = n * 1.0;
        double ans1 = dopow(m,k);
        printf("Case %d: %d %03lld\n",cas, (int)ans1, ans2);
    }
    return 0;
}

Problem

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.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).

Output

For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.

Sample Input

5

123456 1

123456 2

2 31

2 32

29 8751919

Sample Output

Case 1: 123 456

Case 2: 152 936

Case 3: 214 648

Case 4: 429 296

Case 5: 665 669

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-08-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem
  • Input
  • Output
  • Sample Input
  • Sample Output
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档