http://lightoj.com/volume_showproblem.php?problem=1282

Leading and Trailing

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

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

题目大意:给两个数n、k,让求n^k的前三位和后三位

分析:

后三位直接用快数幂取余可以求出

前三位我们可以将n^k转化成a.bc * 10^m,这样abc就是前三位了,n^k =  a.bc * 10^m

即lg(n^k) = lg(a.bc * 10^m)

<==>k * lg(n) = lg(a.bc) + lg(10^m) = lg(a.bc) + m

m为k * lg(n)的整数部分,lg(a.bc)为k * lg(n)的小数部分

x = lg(a.bc) = k * lg(n) - m = k * lg(n) - (int)(k * lg(n))

a.bc = pow(10, x);

abc = a.bc * 100;

这样前三位数abc便可以求出

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std;
typedef long long ll; int Pow(int a, int b)
{
int ans = ;
a %= ;
while(b)
{
if(b % != )
ans = (ans * a) % ;
a = (a * a) % ;
b /= ;
}
return ans;
}//快数幂 int main()
{
int t, n, k, p = ;
scanf("%d", &t);
while(t--)
{
p++;
scanf("%d%d", &n, &k);
double m = k * log10(n) - (int)(k * log10(n));
m = pow(, m);
int x = m * ;
int y = Pow(n, k);
printf("Case %d: %d %03d\n", p, x, y);
}
return ;
}

LightOJ 1282 Leading and Trailing (快数幂 + 数学)的更多相关文章

  1. 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 ...

  2. LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)

    链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...

  3. LightOJ - 1282 Leading and Trailing (数论)

    题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a, ...

  4. LightOj 1282 Leading and Trailing

    求n^k的前三位数字和后三位数字. 范围: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107). 前三位: 设 n^k = x ---> lg(n^k)=lg(x) - ...

  5. LightOJ 1282 Leading and Trailing 数论

    题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...

  6. LightOJ 1282 Leading and Trailing (数学)

    题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后 ...

  7. LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)

    http://lightoj.com/volume_showproblem.php?problem=1213  Fantasy of a Summation Time Limit:2000MS     ...

  8. 1282 - Leading and Trailing 求n^k的前三位和后三位。

    1282 - Leading and Trailing You are given two integers: n and k, your task is to find the most signi ...

  9. 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)

    http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会 ...

随机推荐

  1. 【转载】Windows 7下使用bcdedit删除多余启动项的命令

    在Windows  7中是使用bcdedit来代替Windows XP中的boot.ini bcdedit位置:C:\Windows\System32 (直接使用命令bcdedit即可) bcdedi ...

  2. vijos 1379 字符串的展开

    23333333333333333 #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  3. matlab中,在灰度解剖图上叠加阈值图,by by DR. Rajeev Raizada

    1.参考 reference 1. tutorial主页:http://www.bcs.rochester.edu/people/raizada/fmri-matlab.htm. 2.speech_b ...

  4. Spark 问题总结

    1 创建hive外部表 其实这个问题应该是hive的问题.就是外部表在创建的时候需要指定目录.举例说明 我们要创建一个外部表,其来源是test_tab这个文件,那么在LOCATION处是不是这样写呢? ...

  5. HDU 5001 Walk

    解题思路:这是一道简单的概率dp,只要处理好相关的细节就可以了. dp[d][i]表示走d步时走到i的改概率,具体参考代码: #include<cstdio> #include<cs ...

  6. 关于Java接口

    1 接口的本质 (1)一组有规则的集合: (2)一定视角上的同类事物的抽象:同类事物的概念是相对的 2 接口与抽象类的区别 (1)java不支持类的多继承,但可以实现多个接口: (2)使用动机:抽象类 ...

  7. android adb应用

    一 adb 简介 ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 二 安装 方法 先说安装方法, 电脑上需要安装客户端. 客户端包含在sdk里. ...

  8. String定义与方法

    //5种构造方法 public void Con(){ String str = "sfaj"; String str1 = new String("sfajdf&quo ...

  9. php 采用fpdf乱码问题

    步骤1.首先下载fpdf http://www.fpdf.org/en/download.php(本人用的是1.7版本) 步骤2.下载中文包 http://www.fpdf.org/download/ ...

  10. CSS3制作苹果风格键盘

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtMAAAEICAIAAAASh+8XAAAgAElEQVR4nOzdaXBU14E3/FPVBVUq5X