lightoj 1282 && uva 11029

时间:2023-03-10 06:35:01
lightoj 1282 && uva 11029

Leading and Trailing

lightoj 链接:http://lightoj.com/volume_showproblem.php?problem=1282

uva 链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1970

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

思路1、前 3 位:把 nk 转化为 a.bc * 10,两边取 10 的对数得到 k * lg(n) = m + lg(a.bc)  ( lg(a.bc) < 1 ) 。 所以把 k * lg(n) 减掉整数部分(即 m )就可以得到

lg(a.bc) , 然后计算 10 lg(a.bc) * 100 就能得到答案。

  2、后3位:把 k 化成二进制的数,进行快速幂取模运算,没难度。

代码

lightoj 1282 代码:

 #include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std; typedef long long LL;
int n, k; int front(int n, int k) //前3位
{
double s = k*log10(n) - (int) (k*log10(n)); //取对数
s = pow(, s);
return s * ;
} int rear(int n, int k) //后3位
{
if(!k) return ; //快速幂
LL s = rear(n, k/);
if(k&) s = s*s % * n % ;
else s = s*s % ;
return s % ;
} int main()
{
int t, i;
cin >> t;
for(i = ; i <= t; ++i)
{
scanf("%d%d", &n, &k);
printf("Case %d: %d %03d\n", i, front(n, k), rear(n, k));
}
return ;
}

uva 11029 代码:

 #include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std; typedef long long LL;
int n, k; int front(int n, int k) //前3位
{
double s = k*log10(n) - (int) (k*log10(n)); //取对数
s = pow(, s);
return s * ;
} int rear(int n, int k) //后3位
{
if(!k) return ; //快速幂
LL s = rear(n, k/);
if(k&) s = s*s % * n % ;
else s = s*s % ;
return s % ;
} int main()
{
int t;
cin >> t;
while(t--)
{
scanf("%d%d", &n, &k);
printf("%d...%03d\n", front(n, k), rear(n, k));
}
return ;
}