LightOJ - 1282 Leading and Trailing(数学题)

题目链接:点我点我

题意:给n,k,求nk的前三位和后三位。

题解:后三位直接快速幂。前三位的话,我们假设n=10a,nk=10a*k=10x+y=10* 10y

我们把10x当做位数(就是让他它尽可能大,比如n的k次方为12345,10x就相当于104),10y当做表示的值(12345这个数,它的10y就是1.2345)。

我们把这个10y求出来就可以了,最后再乘上100,强制转换一下,就得到前三位了。但是要怎么求呢。这就要用到之前的公式:

nk=10x+y  <=>  k*log10 n=x+y,因为这时候的y是个小数(就比如说10y是1.2345,y取值显然是 0<y<1),y=double(k*log10 n)- LL(k*log10 n)。

y都求出来了,最后再处理一下就可以了。注意:没有三位的要加前导0.

 1 #include <cmath>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 typedef long long LL;
 6 LL fast_mod(LL x,LL n,LL mod){
 7     LL ans=1;
 8     while(n>0){
 9         if(n&1) ans=(ans*x)%mod;
10         x=(x*x)%mod;
11         n>>=1;
12     }
13     return ans;
14 }
15
16 int main(){
17     LL t,n,k,Case=1;
18     scanf("%lld",&t);
19     while(t--){
20         scanf("%lld %lld",&n,&k);
21         double x=1.0*k*log10(n*1.0);
22         x=x-LL(x);
23         LL y=fast_mod(n,k,1000);
24         printf("Case %lld: %03lld %03lld\n",Case++,(LL)(pow(10,x)*100),y);
25     }
26     return 0;
27 }
时间: 2024-08-09 01:57:51

LightOJ - 1282 Leading and Trailing(数学题)的相关文章

LightOJ 1282 Leading and Trailing (快数幂 + 数学)

http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1282 Description You are given two integers: n and k, your task is t

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 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 st

LightOJ 1282 - Leading and Trailing (求n的k次方的前三位数字 后三位)

题意:http://www.lightoj.com/volume_showproblem.php?problem=1282 n^k = a.bc * 10.0^m:等式两边同时加上log10k*log10(n) = log10(a.bc) + m;m为k * log10(n)的整数部分,log10(a.bc)为k * lg(n)的小数部分;x = log10(a.bc) = k*log10(n) - m = k*log10(n) - (int)k*log10(n);x = pow(10.0, x

LightOJ - 1282 Leading and Trailing (数论)

题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a,则nk=10ak=10x+y,x为ak的整数部分,y为ak的小数部分. eg:n=19,k=4,则nk=130321, a=log10(n)=1.2787536009528289615363334757569 ak=5.1150144038113158461453339030277, 因此,x=5,

LightOJ 1282 Leading and Trailing (数学)

题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后取前三位. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #inc

LightOJ 1282 Leading and Trailing 数论

题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x*10^y,其中10^x为10的整倍数(x为整数确定数位长度),所以主要求出10^y的值. T=log10(n^k)=klog10(n),可以调用fmod函数求其小数部分即y值. #include<iostream> #include<algorithm> #include<cst

LightOJ - 1282 -Leading and Trailing

题目链接:https://vjudge.net/problem/LightOJ-1282 题目大意: 给出一个数n,求n^k次方后的数的前三位和后三位的值. 题目分析: 对于数的后三位可以直接用快速幂取模的方法求得. 求数的前三位,可以先对n^k取log10,此时得到 x=k*log10(n),即10^x=n^k. x=a(x的整数部分)+b(x的小数部分); 此时a决定的是小数点的位置,b决定的是具体的数. 为了求得前三位的数,只需要求得pow(10,2+b)的值即可. 给出代码: 1 #in

LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)

链接: https://vjudge.net/problem/LightOJ-1282 题意: 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. 思路: 后三位快速幂取余,考虑前三位. \(n^k\)可以表示为\(a*10^m\)即使用科学计数法. 对两边取对数得到\(k*log

1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)

http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会快速幂,后来慢慢理解了. 前三位求得比较厉害 我们可以吧n^k = a.bc * 10.0^m; k*log10(n)  = log10(a.bc) + m; m为k * lg(n)的整数部分,lg(a.bc)为k * lg(n)的小数部分; x = log10(a.bc) = k*log10(n)