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

5

123456 1

123456 2

2 31

2 32

29 8751919

Sample Output

Case 1: 123 456

Case 2: 152 936

Case 3: 214 648

Case 4: 429 296

Case 5: 665 669

思路:

n^k的后三位用快速幂。前三位计算方法:指数级一般用对数解决,令x为a^k整数部分, y为a^k小数部分 ,所以10^(x+y)==n^k,其中10^x是10.....0,那么10^y其实就是各个位数上的值,所以10^y前三位就是n^k前三位

新学了一个函数 double a=modf(double x,double *i ),返回x的整数部分给i,小数部分给a

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
const int N=10000003; //18
const int MOD=1000;
using namespace std;
int pow_mod(ll a,ll b){
ll ans=1;
while(b){
if(b&1) ans=ans*a%MOD;
a=a*a%MOD;
b/=2;
}
return (int)ans;
}
int main(){
int T,res1,res2,num=1;
ll a,k;
double y;
double x;
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&a,&k);
res2=pow_mod(a,k);
y=modf((double)(k*log10(a)),&x);
res1=floor(pow(10,y)*100);
printf("Case %d: %d %03d\n",num++,res1,res2);
}
return 0;
}

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

  3. LightOJ 1282 Leading and Trailing 数论

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

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

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

  5. UTF-8格式txt文件读取字节前三位问题

    今天试着读取一份UTF-8格式的txt文件,内容如下 12345 但是每次读取之后转为String类型,输出字符串长度总是为6,并且第一位打印在控制台后不占任何空间. 经过debug查看字节码后发现, ...

  6. UVA - 11029 输出前三位

    题意:给定\(a\)和\(n\),输出\(a^n\)的前三位和后三位 后三位快速幂 \(log_{10}(a^n)=n*log_{10}(a)=n*log_{10}(x*y),y<10,x mo ...

  7. Java生成前三位是字母循环的字典

    title: Java生成前三位是字母循环的字典 date: 2018-08-17 18:52:22 tags: Java --- 最近要破解一个秘密,还好这个密码是有线索的,已知密码的前三位是三个字 ...

  8. Codeforces Round #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  9. Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) (前三题题解)

    这场比赛好毒瘤哇,看第四题好像是中国人出的,怕不是dllxl出的. 第四道什么鬼,互动题不说,花了四十五分钟看懂题目,都想砸电脑了.然后发现不会,互动题从来没做过. 不过这次新号上蓝名了(我才不告诉你 ...

随机推荐

  1. 洛谷P3209平面图判定 [HNOI2010] 2-sat

    正解:2-sat(并茶几/强连通分量 解题报告: 传送门w 难受死了,连WA5次,正确率又-=INF了QAQ 然后先说下这题怎么做再来吐槽自己QAQ 首先这题其实和NOIp2010的关押罪犯挺像的,然 ...

  2. 第五课 JAVA反射获取对象属性和方法(通过配置文件)

    Service1.java package reflection; public class Service1 { public void doService1(){ System.out.print ...

  3. Shell 和Python的区别。

    shell 应该属于宏语言,顾名思义是系统的壳,方便与系统交互的在以下情况下,不使用shell,因为shell对此无能为力:如:跨平台,较复杂数学操作(如浮点运算,精确运算等),图形化界面 GUI,I ...

  4. ASCII对照表

    ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ! 65 A 97 a 2 ST ...

  5. 验证 Googlebot (检查是否为真的Google机器人)

    您可以验证访问您服务器的网页抓取工具是否确实是 Googlebot(还是其他 Google 用户代理).如果您担心自称是 Googlebot 的垃圾内容发布者或其他麻烦制造者访问您的网站,则会发现该方 ...

  6. 软件包管理:rpm命令管理-包命名与依赖性

    rpm包的管理主要有两种方法:一种是rpm命令管理另一种是yum在线管理 注意软件包没有扩展名,写上只是为了好看,便于识别而已. 注意区别包名,包全名.之所以要区分,就是因为有些命令十分挑剔,需要跟正 ...

  7. 通用Dao方法

    import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DatabaseMetaData; import ...

  8. Javascript-逻辑运算符非(!)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. BinarySearch

    今天看代码,看到这么一段,开始没有看明白,记录下来备忘 foreach (FinancialReport r3 in addAorList)            {                i ...

  10. C/C++之宏、内联函数和普通函数的区别

    内联函数的执行过程与带参数宏定义很相似,但参数的处理不同.带参数的宏定义并不对参数进行运算,而是直接替换:内联函数首先是函数,这就意味着函数的很多性质都适用于内联函数,即内联函数先把参数表达式进行运算 ...