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

分析:后三位直接快速幂取余得,对于体格给定的整数n可以写成n = 10^a形式,其中a是浮点数, n ^ k = (10 ^ a) ^ k = (10 ^ x) * (10 ^ y), 其中x,y分别为ak的整数部分和小数部分,对于t=n^k这个数,他的位数由10^x决定,他的位数上的值由10^y决定。因此我们要求t的前三位,只用求出10^y就可以了。

fmod() 用来对浮点数进行取模(求余),其原型为:
    double fmod (double x);

设返回值为 ret,那么 x = n * y + ret,其中 n 是整数,ret 和 x 有相同的符号,而且 ret 的绝对值小于 y 的绝对值。如果 x = 0,那么 ret = NaN。

fmod 函数计算 x 除以 y 的 f 浮点余数,这样 x = i*y + f,其中 i 是整数,f 和 x 有相同的符号,而且 f 的绝对值小于 y 的绝对值。

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>

using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
const int mod = 1000;

int quickmi(int a, int b)
{
if(b == 0)
return 1;

int tmp = quickmi(a, b>>1);

tmp = tmp * tmp % mod;

if(b & 1)
tmp = tmp * (a % mod) % mod;

return tmp % mod;

}
int main(void)
{
int T, cas;
int n, m;

scanf("%d", &T);

cas = 0;

while(T--)
{

cas++;

scanf("%d%d", &n, &m);

int last = quickmi(n % 1000, m);

double y = 2.0 + fmod(m * log10(n * 1.0), 1);
int first = pow(10.0, y);

printf("Case %d: %03d %03d\n", cas, first, last);

}

return 0;
}

1282 - Leading and Trailing 求n^k的前三位和后三位。的更多相关文章

  1. E - Leading and Trailing 求n^k得前三位数字以及后三位数字,保证一定至少存在六位。

    /** 题目:E - Leading and Trailing 链接:https://vjudge.net/contest/154246#problem/E 题意:求n^k得前三位数字以及后三位数字, ...

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

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

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

    http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会 ...

  4. LightOJ 1282 Leading and Trailing 数论

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

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

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

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

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

  7. LightOJ 1282 Leading and Trailing (数学)

    题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后 ...

  8. LightOJ - 1282 Leading and Trailing (数论)

    题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a, ...

  9. ACM_求N^N的前5位数和后5位数(数论)

    NNNNN Time Limit: 2000/1000ms (Java/Others) Problem Description: 对于整数N,求N^N的前5位和后5位(1057题加强版) Input: ...

随机推荐

  1. dfs序 + 树状数组

    You are given a tree, it’s root is p, and the node is numbered from 1 to n. Now define f(i) as the n ...

  2. “Your build settings specify a provisioning profile with the UUID “”, however, no such provisioning profile was found”

    解决方法: 终端命令行输入下面语句,删除所有的Profilescd ~/Library/MobileDevice/Provisioning\ Profiles/rm *.mobileprovision

  3. Java单体应用 - Markdown - 03.高级技巧

    原文地址:http://www.work100.net/training/monolithic-markdown-advance.html 更多教程:光束云 - 免费课程 高级技巧 序号 文内章节 视 ...

  4. SpringBoot中对SpringMVC的自动配置

    https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developin ...

  5. Isx个人第4次作业—Alpha项目测试

    标题 内容 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业要求在哪里 https:// ...

  6. [CCPC2019 ONLINE]E huntian oy

    题意 http://acm.hdu.edu.cn/showproblem.php?pid=6706 思考 打表出奇迹. 注意到这个式子有一大堆强条件限制,最后化为: $$\frac{1}{2}\sum ...

  7. CSS-07-CSS文本设置

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. mac安装了anaconda但是在终端不能使用conda命令

    只需在终端输入如下命令即可 export PATH=~/anaconda3/bin:$PATH

  9. 云原生 - Istio可观察性之分布式跟踪(三)

    作者:justmine 头条号:大数据与云原生 微信公众号:大数据与云原生 创作不易,在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处. 为了方便阅读,微信公众号已按分类排版,后续的文 ...

  10. 线程池之 ThreadPoolExecutor

    线程池之 ThreadPoolExecutor + 面试题 线程池介绍 线程池(Thread Pool):把一个或多个线程通过统一的方式进行调度和重复使用的技术,避免了因为线程过多而带来使用上的开销. ...