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 ;
}

lightoj 1282 && uva 11029的更多相关文章

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

    http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Me ...

  2. Leading and Trailing LightOJ - 1282 题解

    LightOJ - 1282 Leading and Trailing 题解 纵有疾风起 题目大意 题意:给你一个数n,让你求这个数的k次方的前三位和最后三位. \(2<=n<2^{31} ...

  3. 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 ...

  4. uva 11029

    看了别人的解法 发现了 modf 这个函数 取小数部分 /*********************************************************************** ...

  5. LightOj 1282 Leading and Trailing

    求n^k的前三位数字和后三位数字. 范围: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107). 前三位: 设 n^k = x ---> lg(n^k)=lg(x) - ...

  6. LightOJ 1282 Leading and Trailing 数论

    题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...

  7. Uva 11029 Leading and Trailing (求n^k前3位和后3位)

    题意:给你 n 和 k ,让你求 n^k 的前三位和后三位 思路:后三位很简单,直接快速幂就好,重点在于如何求前三位,注意前导0 资料:求n^k的前m位 博客连接地址 代码: #include < ...

  8. lightoj 1282 取对数的操作

    /* 前三位 len=log10n^k(乘积的长度) len=klog10n n^k=x*10^(len-1) x=n^k/10^(len-1) log10x = k*log10n - (len-1) ...

  9. Leading and Trailing LightOJ - 1282 (取数的前三位和后三位)

    题意: 求n的k次方的前三位 和 后三位 ...刚开始用 Java的大数写的...果然超时... 好吧  这题用快速幂取模求后三位  然后用一个技巧求前三位 ...orz... 任何一个数n均可以表示 ...

随机推荐

  1. cronolog:日志分割工具

    一. 引言 因为tomcat的catalina.out日志无法按照日期自动创建,因此采用cronnlog分割. 二. 安装与配置 1.安装cronolog: yum install -y cronol ...

  2. 试用Markdown来写东西

    试用Markdown来写东西 前言 之前有过一段时间的写东西的习惯,但是后来因为各种原因(主要是因为自己懒惰拖延),所以一直没有写,现在想再开始写,目的很明确,就是发现很多时候,写作能够很好的练习自己 ...

  3. php redis和java混用问题

    目前项目是 一个php 一个java  共用一套 redis  key  value 也都一样,  java 使用 gson 解析json   会将php 设置的json里面看  {"a&q ...

  4. Struts2文件上传带进度条,虽然不是很完美

    好久没有写东西,最近在做个项目,要用到文件h 传的,以前虽然也做上传,但是总觉得不好用 ,现在和队友合作做了一个带进度条的上传,觉得还行~~和大家分享一下. 首先说一下大概是这样实现的,在我们平时的上 ...

  5. POJ 2177 Ghost Busters(三维几何)

    Description The famous Ghost Busters team has decided to upgrade their Ectomobile (aka Ecto-1) with ...

  6. Advanced Fruits (最大公共子序列的路径打印)

    The company "21st Century Fruits" has specialized in creating new sorts of fruits by trans ...

  7. lintcode-34-N皇后问题 II

    34-N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 标签 递归 思路 参考http://www.cnblogs.com ...

  8. 3dContactPointAnnotationTool开发日志(三)

      今天的目的是把obj文件导到场景里.具体将制定路径的obj文件导进去我用的是这个方法.导进去后呈现的是一个黑色的影子.   导入后还想实现一下缩放功能,请看这个方法.缩放实现起来也很简单.   光 ...

  9. PAT 1035 插入与归并

    https://pintia.cn/problem-sets/994805260223102976/problems/994805286714327040 据维基百科的定义: 插入排序是迭代算法,逐一 ...

  10. Swift & Unicode

    Swift & Unicode emoji let == const https://www.runoob.com/swift/swift-basic-syntax.html