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