Light OJ 1282 Leading and Trailing (对数+快速幂)
来源:互联网 发布:盒子设计软件绿色版 编辑:程序博客网 时间:2024/06/07 15:14
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
5123456 1123456 22 312 3229 8751919
Sample Output
Case 1: 123 456Case 2: 152 936Case 3: 214 648Case 4: 429 296Case 5: 665 669
题目大意:
求出一个数的n的k次方结果的前三位和后三位
解题思路:
(1)后三位用快速幂取一下就好了
(2)前三位利用对数,公式如下
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<bits/stdc++.h>using namespace std;#define mod 1000long long quick_mod(long long a,long long n) //快速幂取模结果的求后三位 { long long res=1; while(n) { if(n%2) res=res*a%mod; n>>=1; a=a*a%mod; } return res;}int main(){ long long ans,tt=0,t,n,m; cin>>t; while(t--) { cin>>n>>m; double c=m*log10(n)-(long long)(m*log10(n)); //去对数的小数部分 并用科学计数法表示 ans=pow(10,c+2); //扩大100倍 printf("Case %lld: %lld %03lld\n", ++tt,ans,quick_mod(n,m)); } return 0;}
0 0
- Light OJ 1282 Leading and Trailing (对数+快速幂)
- Light OJ:1282 Leading and Trailing(数学+思维+对数+快速幂)
- 【Light-oj】-1282 - Leading and Trailing(数论,快速幂,log,好)
- 【Light oj】1282 - Leading and Trailing
- E - Leading and Trailing Light oj 1282
- Light-oj 1282 - Leading and Trailing
- Light OJ 1282 Leading and Trailing
- light oj-1282 Leading and Trailing
- Light-oj-1282 Leading and Trailing(数学取位)
- Light OJ 1282(B)——Leading and Trailing
- LIGHToj 1282 - Leading and Trailing 【对数+快速幂取模】
- LightOJ - 1282 Leading and Trailing 对数转换
- 【LightOJ】1282 - Leading and Trailing(快速幂 & 数论)
- Light OJ-----1282对数快速幂
- LightOJ - 1282Leading and Trailing (快速幂简单数论求幂数前几位)
- LightOJ 1282 - Leading and Trailing(快速幂取模)
- uva 11029 - Leading and Trailing(快速幂)
- UVA - 11029 - Leading and Trailing (快速幂+公式变形)
- 51. N-Queens
- 52. N-Queens II
- 53. Maximum Subarray
- POJ3268【最短路】
- 54. Spiral Matrix
- Light OJ 1282 Leading and Trailing (对数+快速幂)
- I AM ARRIVAL, I AM COMING.
- 55. Jump Game
- 56. Merge Intervals
- 57. Insert Interval
- 58. Length of Last Word
- hdu 1709 The Balance(母函数)
- 59. Spiral Matrix II
- 60. Permutation Sequence