light_oj 1282 求n^k的前几位数和后几位数
来源:互联网 发布:游戏王卡牌diy软件 编辑:程序博客网 时间:2024/05/29 18:52
light_oj 1282 求n^k的前几位数和后几位数
light_oj 1282 求n^k的前几位数和后几位数
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=a.bc*10^m ( m为n^k的位数,即m=(int)lg(n^k)=(int)(k*lgn) );
求对数: k*lgn=lg(a.bc)+m
即 a.bc=10^(k*lgn-m)=10^(k*lgn-(int)(k*lgn));
abc=a.bc*100;
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<set>#include<map>#include<string>#include<math.h>#include<cctype>using namespace std;typedef long long ll;const int maxn=1000100;const int INF=(1<<29);const double EPS=0.0000000001;const double Pi=acos(-1.0);const int p=1000;ll n,k;ll qpow(ll n,ll k){ ll res=1; while(k){ if(k&1) res=(res%p)*(n%p)%p; n=(n%p)*(n%p)%p; k>>=1; } return res;}ll f(ll n){ double x=k*log10(n)-(int)(k*log10(n)); return pow(10,x)*100;}int main(){ int T;cin>>T; int tag=1; while(T--){ cin>>n>>k; printf("Case %d: %lld %03lld\n",tag++,f(n),qpow(n,k)); } return 0;}
- light_oj 1282 求n^k的前几位数和后几位数
- 求n^k的前几位数和后几位数
- a的b次方的前N位数和后N位数
- 获取后几位数
- 【经典例题】高次幂的后几位数
- 求N!的位数
- 求N!的位数
- 求N!的位数
- 保留小数点后几位数
- 求一数的N次方的最后几位数(待续)
- 求大数n!的位数
- PKU1423:求n!的位数。
- 求n^k的前三位数和后三位数
- 求大数最左边的某几位数
- 几位数乘法的结构
- 给定N位数,去掉K位数,得到的N-K位数中最小的那个数?
- LightOJ 1282 - Leading and Trailing (求n^k的前三位和后三位)
- 几种查看CentOS系统版本和位数的方法
- 线程间的通信机制
- 动态获取cell高度
- html+css+js基础知识
- 6.2 ipu_csi_enc.c详细分析
- java自定义注解入门
- light_oj 1282 求n^k的前几位数和后几位数
- 理解Java 8 Streams API,Collector,Parallelism
- 在PetShop 4.0中ASP.NET缓存的实现
- JS/jquery的一些常用功能函数
- wordpress multisite network
- js检测移动端(先存着)
- Java知识点24
- 7.1 ipu_device.c分析(一)---流程分析
- Linux基本命令练习(10)---wget、less