LightOJ 1282

Leading and Trailing

Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

Submit Status

Description

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

Source

Problem Setter: Shamim Hafiz

Special Thanks: Jane Alam Jan (Solution, Dataset)

/**
          题意:求a的b次幂,结果的高三位和低三位是什么
          做法:快速幂
**/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<algorithm>
#define mod 1000
using namespace std;
int quickpow(int n, int k)
{
    int res = 1;
    int base = n%mod;
    while(k)
    {
        if(k&1)
            res *= base;
        base *= base;
        res %= mod;
        base %= mod;
        k >>= 1;
    }
    return res;
}
int solve(int   a,int   b)
{
          double s = b*log10((double)a) - (int)(b*log10((double)a));
    s = pow(10.0,s);
    return s*100;
}
int main()
{
#ifndef ONLINE_JUDGE
         freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
          int Case = 1;
          int T;
          scanf("%d",&T);
          while(T--)
          {
                    int  a,b;
                    scanf("%d %d",&a,&b);
                    int  res = quickpow(a,b);
                    int cet =pow(10, 2+fmod(b *(double)log10((double)a), 1));
                   printf("Case %d: %d %03d\n",Case,cet,res);
                   Case++;
          }
          return 0;
}
时间: 2024-10-17 22:57:36

LightOJ 1282的相关文章

LightOJ 1282 Leading and Trailing (快数幂 + 数学)

http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1282 Description You are given two integers: n and k, your task is t

快速幂 E - Leading and Trailing LightOJ - 1282

E - Leading and Trailing LightOJ - 1282 快速幂主要是把n拆成2进制位,如果这一位有那么就乘,没有就不乘,而计数器也就是x是不断推进的,从x->x^2->x^4直到n的最高位精髓在于取模,后一步的要求结果只与前一步的模后数据有关 . 对于后三个数用了log10.log函数对求n^k这种问题还是很有用的.没想出来. 1 #include <iostream> 2 #include <cstring> 3 #include <st

LightOJ 1282 - Leading and Trailing (求n的k次方的前三位数字 后三位)

题意:http://www.lightoj.com/volume_showproblem.php?problem=1282 n^k = a.bc * 10.0^m:等式两边同时加上log10k*log10(n) = log10(a.bc) + m;m为k * log10(n)的整数部分,log10(a.bc)为k * lg(n)的小数部分;x = log10(a.bc) = k*log10(n) - m = k*log10(n) - (int)k*log10(n);x = pow(10.0, x

LightOJ - 1282 Leading and Trailing (数论)

题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a,则nk=10ak=10x+y,x为ak的整数部分,y为ak的小数部分. eg:n=19,k=4,则nk=130321, a=log10(n)=1.2787536009528289615363334757569 ak=5.1150144038113158461453339030277, 因此,x=5,

LightOJ - 1282 Leading and Trailing(数学题)

题目链接:点我点我 题意:给n,k,求nk的前三位和后三位. 题解:后三位直接快速幂.前三位的话,我们假设n=10a,nk=10a*k=10x+y=10x  * 10y. 我们把10x当做位数(就是让他它尽可能大,比如n的k次方为12345,10x就相当于104),10y当做表示的值(12345这个数,它的10y就是1.2345). 我们把这个10y求出来就可以了,最后再乘上100,强制转换一下,就得到前三位了.但是要怎么求呢.这就要用到之前的公式: nk=10x+y  <=>  k*log1

LightOJ 1282 Leading and Trailing (数学)

题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后取前三位. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #inc

UVA 11029 || Lightoj 1282 Leading and Trailing 数学

Leading and Trailing 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 st

LightOJ 1282 Leading and Trailing 数论

题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x*10^y,其中10^x为10的整倍数(x为整数确定数位长度),所以主要求出10^y的值. T=log10(n^k)=klog10(n),可以调用fmod函数求其小数部分即y值. #include<iostream> #include<algorithm> #include<cst

LightOJ - 1282 -Leading and Trailing

题目链接:https://vjudge.net/problem/LightOJ-1282 题目大意: 给出一个数n,求n^k次方后的数的前三位和后三位的值. 题目分析: 对于数的后三位可以直接用快速幂取模的方法求得. 求数的前三位,可以先对n^k取log10,此时得到 x=k*log10(n),即10^x=n^k. x=a(x的整数部分)+b(x的小数部分); 此时a决定的是小数点的位置,b决定的是具体的数. 为了求得前三位的数,只需要求得pow(10,2+b)的值即可. 给出代码: 1 #in