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 ;
}
UVA 11029 || Lightoj 1282 Leading and Trailing 数学的更多相关文章
- LightOJ 1282 Leading and Trailing (数学)
题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后 ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- LightOJ 1282 Leading and Trailing (快数幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS Me ...
- light OJ 1282 - Leading and Trailing 数学 || double技巧
http://lightoj.com/volume_showproblem.php?problem=1282 #include <cstdio> #include <cstdlib& ...
- LightOj 1282 Leading and Trailing
求n^k的前三位数字和后三位数字. 范围: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107). 前三位: 设 n^k = x ---> lg(n^k)=lg(x) - ...
- LightOJ 1282 Leading and Trailing 数论
题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...
- LightOJ - 1282 Leading and Trailing (数论)
题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a, ...
- 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 ...
- 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会 ...
随机推荐
- 【BZOJ1912】[Apio2010]patrol 巡逻 树形DP
[BZOJ1912][Apio2010]patrol 巡逻 Description Input 第一行包含两个整数 n, K(1 ≤ K ≤ 2).接下来 n – 1行,每行两个整数 a, b, 表示 ...
- NodeJS版本EasyDarwin开源流媒体服务器开发心得
title: Node版本EasyDarwin开发心得 date: 2018-03-27 22:46:15 tags: 年后着手Node版本EasyDarwin的开发工作,截止到今天2018年03月2 ...
- 记录-java执行请求的URL
package wzh.Http; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...
- 实用的 集合工具类 和 String工具类
集合工具类:CollectionUtil method: 1.isNotEmpty() 不为空 2.isEmpty() 为空 举例:map集合 Map<String,String ...
- ORACLE中的MERGE语法使用记录
项目中使用到了Oracle的MERGE INTO语句,在这里简单记录下使用方法 使用场景如下: 存在对一张数据量很大的表,你需要对里面的大量数据进行更新,如果数据不存在,就进行插入的操作. 常规想到的 ...
- Display BLOBs and CLOBs (DB2可视化工具AQT )
Displaying BLOBs and CLOBs Dealing with LOBs BLOB and CLOB columns are difficult to display because: ...
- Testlink安装访问提示“应用程序DEFAULT WEB SITE”中的服务器错误
错误摘要:HTTP错误403.14 - ForbiddenWeb服务器被配置为不列出此目录的内容.
- HTML 之 Table 表格详解
HTML 之 Table 表格详解 HTML中的table可以大致分为三个部分: thead ---------表格的页眉 tbody ---------表格的主体 tfoot ---------定义 ...
- nginx反向代理三台web
1.首先我们需要在服务器中三个不同名字,并将他们赋值 2.切换到nginx—conf 把三台机器的nginx的配置文件分别命名为web1.conf.web2.conf.web3.conf vim的赋 ...
- 剑指offer 面试15题
面试15题: 题目:二进制中1的个数 题:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 解题思路一: 最佳方法:把一个整数减去1,再和原整数做“与运算”,会把该整数最右边的1变成0 ...