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

UVA 11029 || Lightoj 1282 Leading and Trailing 数学的更多相关文章

  1. LightOJ 1282 Leading and Trailing (数学)

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

  2. LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)

    链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...

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

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

  4. light OJ 1282 - Leading and Trailing 数学 || double技巧

    http://lightoj.com/volume_showproblem.php?problem=1282 #include <cstdio> #include <cstdlib& ...

  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. LightOJ - 1282 Leading and Trailing (数论)

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

  8. 1282 - Leading and Trailing 求n^k的前三位和后三位。

    1282 - Leading and Trailing You are given two integers: n and k, your task is to find the most signi ...

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

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

随机推荐

  1. android菜鸟学习笔记26----Android广播消息及BroadcastReceiver

    1.广播类型: Android中的广播有两种类型:标准广播和有序广播.其中,标准广播是完全异步发送的广播,发出之后,几乎所有的广播接收者都会在同一时刻收到这条广播消息,因而,这种类型的广播消息是不可拦 ...

  2. VS2010编译报错FileTracker error FTK1011

    系统重装,TFS重新映射,编译项目报错,出现 FileTracker error FTK1011,折腾半天未搞定,网上找到答案,可能是路径更改导致(未验证): 修改MSNET.Framework 目标 ...

  3. Linux中的日志分析及管理

    日志文件对于诊断和解决系统中的问题很有帮助,因为在Linux系统中运行的程序通常会把系统消息和错误消息写入相应的日志文件,这样系统一旦出现问题就会“有据可查”.此外,当主机遭受攻击时,日志文件还可以帮 ...

  4. Linux下Solr的安装和配置

    一.安装 1.需要的安装包:apache-tomcat-7.0.47.tar.gz.solr-4.10.3.tgz.tgz(jdk自行安装) 2.解压tomcat并创建solr文件夹 [root@lo ...

  5. 【转】Power System 中基于 VIOS 的虚拟以太网实现

    基于 VIOS 的虚拟以太网适配器的工作原理和配置实现 本文对 Power 系统中基于 VIOS 的虚拟以太网适配器(Virtual Ethernet Adapter)的工作原理.基本配置选项和配置步 ...

  6. 怎样解决Please ensure that adb is correctly located at......

    昨天下午搭建了Android开发环境,但是天公不作美--执行新建的Android项目总是提演示样例如以下问题: [2014-10-30 15:41:47 - ] The connection to a ...

  7. 024-Spring Boot 应用的打包和部署

    一.概述 二.手工打包[不推荐] 打包命令:maven clean package 打包并导出依赖:maven clean package dependency:copy-dependencies 1 ...

  8. GAN综述

    生成式对抗模型GAN (Generativeadversarial networks) 是Goodfellow等[1]在 2014年提出的一种生成式模型,目前已经成为人工智能学界一个热门的研究方向,著 ...

  9. mybatis-generator和TKmybatis的结合使用

    mybatis-generator可以自动生成mapper和entity文件,mybatis-generator有三种用法:命令行.eclipse插件.maven插件.这里使用的是maven插件方式, ...

  10. Google Cloud Platfrom中运行基础的Apache Web服务

    Links: https://cloud.google.com/compute/docs/tutorials/basic-webserver-apache 步骤: 1.安装Apache 2.重写Apa ...