light_oj 1282
2017-08-15 10:42
204 查看
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
题意:有T组数据,每组有n和k,求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;
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
题意:有T组数据,每组有n和k,求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 <stdio.h> #include <string.h> #include <math.h> typedef long long ll; ll n; ll F(ll a,ll b) { ll res; if(b==0) return 1; res=F(a,b/2)%1000; res=res*res%1000; if(b%2) res=res*a%1000; return res; } int main() { int t,Case=1,k; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&k); double x=k*log10(n)-(int)(k*log10(n)); ll term=pow(10,x)*100; printf("Case %d: %lld %03lld\n",Case++,term,F(n%1000,k)); } return 0; }
相关文章推荐
- 【Light-oj】-1282 - Leading and Trailing(数论,快速幂,log,好)
- light_oj 1282 求n^k的前几位数和后几位数
- Light-oj-1282 Leading and Trailing(数学取位)
- E - Leading and Trailing Light oj 1282
- Light-oj 1282 - Leading and Trailing
- 【Light-oj】-1198 - Karate Competition(贪心+双端队列)
- 【light-oj】-1109 - False Ordering(数学)
- 【Light-oj】-Integer Divisibility(同余定理)
- B - Discovering Gold Light oj 1030
- 【算法学习笔记】83.排序辅助 动态规划 SJTU OJ 1282 修路
- 【Light-oj】-1138 - Trailing Zeroes (III)(二分,数学)
- 【light-oj】-’1307 - Counting Triangles(二分)
- Light-oj-1045 lightoj1045 - Digits of Factorial (N!不同进制的位数)
- light--oj--1078 - Integer Divisibility
- Light oj Trailing Zeroes (III) (二分查找)
- 【Light-oj】-1043 - Triangle Partitioning(数学)
- 【Light-oj】-1414 - February 29(容斥原理)
- 【Light-oj】-Expanding Rods(二分&几何)
- 区间dp||记忆化搜索 Game of Sum UVA - 10891 or Light OJ 1031---Easy Game(区间DP)
- 【light-oj】-1189 - Sum of Factorials(思维,数学)