快速幂 E - Leading and Trailing LightOJ - 1282

E - Leading and Trailing

LightOJ - 1282

快速幂主要是把n拆成2进制位,如果这一位有那么就乘,没有就不乘,而计数器也就是x是不断推进的,从x->x^2->x^4直到n的最高位
精髓在于取模,后一步的要求结果只与前一步的模后数据有关 。

对于后三个数用了log10.log函数对求n^k这种问题还是很有用的。没想出来。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <map>
 5 #include <set>
 6 #include <algorithm>
 7 #include <fstream>
 8 #include <cstdio>
 9 #include <cmath>
10 #include <stack>
11 #include <queue>
12 using namespace std;
13 const double Pi=3.14159265358979323846;
14 typedef long long ll;
15 const int MAXN=5000+5;
16 const int dx[5]={0,0,0,1,-1};
17 const int dy[5]={1,-1,0,0,0};
18 const int INF = 0x3f3f3f3f;
19 const int NINF = 0xc0c0c0c0;
20 ll mod_pow(ll n,ll x,ll mod)
21 {
22     ll ans=1;
23     while(n>0){
24         if(n&1) ans=ans*x%mod;
25         x=x*x%mod;
26         n>>=1;
27     }
28     return ans;
29 }
30
31 int main()
32 {
33     int cnt=0;
34     int t;cin>>t;
35     while(t--)
36     {
37         ll n,k;cin>>n>>k;
38         ll last=mod_pow(k,n,1000);
39         double p=k*log10(n);
40         ll x=(ll)p;double y=p-x;
41         y=pow(10,y)*100;
42         ll first=int(y);
43         printf("Case %d: %03lld %03lld\n",++cnt,first,last);
44     }
45     return 0;
46  } 

原文地址:https://www.cnblogs.com/Msmw/p/10991172.html

时间: 2024-11-10 17:14:47

快速幂 E - Leading and Trailing LightOJ - 1282的相关文章

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

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

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)

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

UVA Leading and Trailing 11029【数学+快速幂】

11029 - Leading and Trailing Time limit: 3.000 seconds Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double da

UVA11029 Leading and Trailing【快速模幂+数学】

Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double data type format, but you won't get all the digits of the

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,求nk的前三位和后三位. 题解:后三位直接快速幂.前三位的话,我们假设n=10a,nk=10a*k=10x+y=10x  * 10y. 我们把10x当做位数(就是让他它尽可能大,比如n的k次方为12345,10x就相当于104),10y当做表示的值(12345这个数,它的10y就是1.2345). 我们把这个10y求出来就可以了,最后再乘上100,强制转换一下,就得到前三位了.但是要怎么求呢.这就要用到之前的公式: nk=10x+y  <=>  k*log1

LightOJ 1282 Leading and Trailing (数学)

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