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. UVa 12206 (字符串哈希) Stammering Aliens

    体验了一把字符串Hash的做法,感觉Hash这种人品算法好神奇. 也许这道题的正解是后缀数组,但Hash做法的优势就是编码复杂度大大降低. #include <cstdio> #inclu ...

  2. hdu 4655 Cut Pieces 找规律

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4655 题意:给你一组整数,代表每个木块所能涂成的颜色种数(编号1~ai),相邻的两块所能涂成的颜色如果是一 ...

  3. Elasticsearch学习笔记

    Why Elasticsearch? 由于需要提升项目的搜索质量,最近研究了一下Elasticsearch,一款非常优秀的分布式搜索程序.最开始的一些笔记放到github,这里只是归纳总结一下. 首先 ...

  4. android studio 加载第三方类库

    以引入Xutil包为例 1. 将Xutil包导入到module的libs目录下 2. File->project structure 还有一种方法是在libs目录下右键点击Add as libr ...

  5. codeigniter框架扩展核心类---实现前台后台视图的分离

    1. 扩展核心类,主要作用就是扩展系统现在的功能. 为前台增加独立的视图文件夹: a. 自定义路径常量 :在application ->config/  constants.php中增加 /*m ...

  6. 初识jQuery 2013-09-26

    常用选择器 $("#bad")  id选择器 $("div#bad")   id为bad 并且必须是div的元素 $("[href]")  ...

  7. python知识:json格式文本;异常处理;字符串处理;unicode类型和str类型转换

    python进程中的实例和json格式的字符串之间的映射关系是非常直接的,相当于同一个概念被编码成不同的表示: stream in json form ----json.loads(str)----- ...

  8. 可接受多个值的文件上传字段HTML5新特性

    <input type="file" id="input"  multiple="multiple"> 主要是多了个multip ...

  9. [Java]获取Window界面的标题栏的高度大小

    利用JFrame继承java.awt.Container类的函数getInsets().该函数返回insets类. import java.awt.Insets; import javax.swing ...

  10. ElasticSearch 查询语法

    ElasticSearch是基于lucene的开源搜索引擎,它的查询语法关键字跟lucene一样,如下: 分页:from/size 字段:fields 排序:sort 查询:query 过滤:filt ...