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.

Sample Input

Output for Sample Input

5

123456 1

123456 2

2 31

2 32

29 8751919

Case 1: 123 456

Case 2: 152 936

Case 3: 214 648

Case 4: 429 296

Case 5: 665 669

题意:求n^k最开始的三个数字和最后的三个数字;

思路:最后的,显然快速幂%1000;注意输出需要前导0;

   开始的用double,快速幂,每次将base在1-10之间,最后的答案*100转int就行了;

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define ll long long
#define esp 1e-13
const int N=1e4+,M=1e6+,inf=1e9+,mod=;
int quickpow(int x,int y)
{
x%=;
int sum=;
while(y)
{
if(y&)sum*=x,sum%=;
x*=x;
x%=;
y>>=;
}
return sum;
}
void change(double &a)
{
while(a-10.0>=(-esp))
{
a/=;
}
}
double pow1(double x,int y)
{
change(x);
double ans=1.0;
while(y)
{
if(y&)ans*=x,change(ans);
x*=x;
change(x);
y>>=;
}
return ans;
}
int main()
{
int x,y,i,z,t;
int T,cas;
scanf("%d",&T);
for(cas=;cas<=T;cas++)
{
scanf("%d%d",&x,&y);
double a=(double)x;
double ans=pow1(a,y);
int yu=quickpow(x,y);
if(yu>=)
printf("Case %d: %d %d\n",cas,(int)(ans*100.0),yu);
else if(yu>=)
printf("Case %d: %d 0%d\n",cas,(int)(ans*100.0),yu);
else if(yu>=)
printf("Case %d: %d 00%d\n",cas,(int)(ans*100.0),yu);
}
return ;
}

最新文章

  1. 传参数应该用哪种形式&mdash;&mdash;值、引用、指针?
  2. struts2运行机制
  3. CF GYM 100703L Many questions
  4. ASP.NET MVC- MvcPager
  5. 手动es6编译es5(命令行)
  6. Java IO流之文件流
  7. Linux 定时任务不生效的问题
  8. 使用@contextmanager装饰器实现上下文管理器
  9. 论MVC中的传值
  10. Spring Boot使用AOP在控制台打印请求、响应信息
  11. CentOS 7 输入中文 &amp; 安装搜狗输入法
  12. 首次使用idea步骤
  13. Qt中 .pro 文件和 .pri 文件简介
  14. PAT A1122 Hamiltonian Cycle (25 分)——图遍历
  15. GUI界面修饰
  16. windows 下的命令操作
  17. lintcode 刷题 by python 部分链表题总结(2)
  18. FastReport 保存为文件
  19. opencv的基本数据结构(二)(转)
  20. log4j示例-Daily方式(log4j.properties)

热门文章

  1. 《从零开始学Swift》学习笔记(Day 17)——Swift中数组集合
  2. 如何理解docker镜像build中的上下文
  3. CSS3 Flex布局(容器)
  4. ffmpeg参数使用说明2
  5. 六百字读懂 Git(转)
  6. python面向对象(二)
  7. 我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计
  8. (2)sql server 跨网段复制
  9. 《Python机器学习》笔记(五)
  10. MongoDB学习笔记—常用命令