LightOJ 1282 Leading and Trailing (快数幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282
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 (快数幂 + 数学)的更多相关文章
- 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 ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- LightOJ - 1282 Leading and Trailing (数论)
题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a, ...
- LightOj 1282 Leading and Trailing
求n^k的前三位数字和后三位数字. 范围: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107). 前三位: 设 n^k = x ---> lg(n^k)=lg(x) - ...
- LightOJ 1282 Leading and Trailing 数论
题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...
- LightOJ 1282 Leading and Trailing (数学)
题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后 ...
- LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)
http://lightoj.com/volume_showproblem.php?problem=1213 Fantasy of a Summation Time Limit:2000MS ...
- 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 ...
- 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会 ...
随机推荐
- (转)IOS UITableView学习
转自:http://www.cnblogs.com/smileEvday/archive/2012/06/28/tableView.html 作者:一片枫叶 看TableView的资 ...
- 使用了BeanUtils的简单操作
直接获取对象的某个值et.createCell(BeanUtils.getProperty(o, eh.getFieldName())); 简单为对象某个字段赋值c.setCellValue(Bean ...
- 20160207.CCPP体系详解(0017天)
程序片段:01.Point.c+02.进程.c+03.内存的栈和堆.c 内容概要:内存32位和64位的区别 ///01.Point.c #include <stdio.h> #includ ...
- 【英语】Bingo口语笔记(24) - L的发音技巧
舌头往上跑
- centos下安装python
下载网址:http://ftp.gnu.org/gnu/gdb/ 1.编译python必须安装开发工具 # yum groupinstall "Development tools" ...
- Docker 基础技术:Linux Namespace(下)
导读 在Docker基础技术:Linux Namespace(上篇)中我们了解了,UTD.IPC.PID.Mount 四个namespace,我们模仿Docker做了一个相当相当山寨的镜像.在这一篇中 ...
- [再寄小读者之数学篇](2014-11-26 广义 Schur 分解定理)
设 $A,B\in \bbR^{n\times n}$ 的特征值都是实数, 则存在正交阵 $P,Q$ 使得 $PAQ$, $PBQ$ 为上三角阵.
- LoadRunner error -27979
4.LoadRunner请求无法找到:在录制Web协议脚本回放脚本的过程中,会出现请求无法找到的现象,而导致脚本运行停止.错误现象:Action.c(41): Error -27979: Reques ...
- JS判断是不是本页面并且,给标签添加属性和属性值
大多是在导航栏中用到的,在导航栏中在主页和在其他的网页点击导航栏中的主页是不同的,主要就是判断这个. 我是在ascs页面中写的. 下面先看标签: <a href="http://www ...
- 【LeetCode】139 - Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...