题解:求一个数的次幂,然后输出前三位和后三位,后三位注意有前导0的情况。 后三位直接用快速幂取模求解。

前三位求得时候只需要稍微变形一下,可以把乘过的结果拆成用科学计数法,那么小数部分只有由前面决定,所以取前三位利用double来计算就可以了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Mod = 1000;
ll ppow(ll a, ll k) // 后三位
{
ll ans = 1;
while(k)
{
if(k%2)ans *= a;
ans %= Mod;
a *= a;
a %= Mod;
k /= 2;
}
return ans;
}
double Merge(double x)
{
while(x >=1000.0)
{
x /= 10.0;
}
return x;
}
double dopow(double a, int k) // 前三位
{
double ans = 1.0;
while(k)
{
if(k%2)ans *= a;
ans = Merge(ans);
a *= a;
a = Merge(a);
k /= 2;
}
return ans;
}
int main()
{
int T,cas = 0;
ll n, k;
scanf("%d",&T);
while(T--)
{
cas ++;
scanf("%lld %lld", &n, &k);
ll ans2 = ppow(n,k);
double m = n * 1.0;
double ans1 = dopow(m,k);
printf("Case %d: %d %03lld\n",cas, (int)ans1, ans2);
}
return 0;
}

Problem

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

Leading and Trailing(LightOJ - 1282)的更多相关文章

  1. 【LightOJ1282】Leading and Trailing(数论)

    [LightOJ1282]Leading and Trailing(数论) 题面 Vjudge 给定两个数n,k 求n^k的前三位和最后三位 题解 这题..真的就是搞笑的 第二问,直接输出快速幂\(m ...

  2. Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)

    Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...

  3. Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】

    Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...

  4. Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】

    Goldbach`s Conjecture(LightOJ - 1259)[简单数论][筛法] 标签: 入门讲座题解 数论 题目描述 Goldbach's conjecture is one of t ...

  5. Leading and Trailing (数论)

    Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k ...

  6. Leading and Trailing(数论/n^k的前三位)题解

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

  7. LightOJ - 1282 Leading and Trailing (数论)

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

  8. Leading and Trailing(巧妙利用log解决次方问题)

    Sample Input 5 123456 1 123456 2 2 31 2 32 29 8751919 Sample Output Case 1: 123 456 Case 2: 152 936 ...

  9. (最长公共子序列+推导)Love Calculator (lightOJ 1013)

    http://www.lightoj.com/volume_showproblem.php?problem=1013   Yes, you are developing a 'Love calcula ...

随机推荐

  1. 利用Python进行数据分析_Numpy_基础_1

    ndarray:多维数组 ndarray 每个数组元素必须是相同类型,每个数组都有shape和dtype对象. shape 表示数组大小 dtype 表示数组数据类型 array 如何创建一个数组? ...

  2. 用pandas库对csv文件中的文本数据进行分析处理

    #数据分析 import pandas import csv old_path = r'd:\2000W\200W-400W.csv' f = open(old_path,'r',encoding=' ...

  3. Reids 连环炮面试(转)

    出处:  <今天面试了吗>-Redis Redis是什么 面试官:你先来说下redis是什么吧 我:(这不就是总结下redis的定义和特点嘛)Redis是C语言开发的一个开源的(遵从BSD ...

  4. PB事件/函数的触发机制和触发方式

    PB作为windows下的一个非常便捷的DB开发工具,有着和windows一样的消息触发机制PB提供了相应event/function触发机制和触发方式,用户可以根据自己的实际需要选用不同方法. 1. ...

  5. 怎样通过CSS选择器获取元素节点或元素节点集合

    使用 document.querySelector() 和 document.querySelectorAll(), 将 CSS选择器 作为参数传入即可. // 标签选择器 document.quer ...

  6. Typeof() 和 GetType()区别

    1.typeof(x)中的x,必须是具体的类名.类型名称等,不可以是变量名称. 2.GetType()方法继承自Object,所以C#中任何对象都具有GetType()方法,它的作用和typeof() ...

  7. SQLSERVER 20018 R2 T-SQL 创建linkServer

    1. SQLSERVER LINK SQLSERVER EXEC sp_addlinkedserver @server = 'LINKTEST',@srvproduct = '',@provider ...

  8. NVIDIA双显卡

    NVIDIA双显卡 第一步:代码:sudo update-pciids #更新显卡信息非常重要,否则可能识别出错lspci -v | grep -i vga #察看显卡 我的显卡信息如下:代码:00: ...

  9. HTTP请求方式及其区别

    一.请求方式 所有的请求都可以给服务器传递内容,也可以从服务器获取内容. GET:从服务器获取数据(给的少拿的多) POST:向服务器推送数据(给的多拿的少) DELETE:删除服务器的一些内容 PU ...

  10. JAVA栅栏和闭锁的区别

    闭锁:一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.即,一组线程等待某一事件发生,事件没有发生前,所有线程将阻塞等待:而事件发生后,所有线程将开始执行:闭锁最初 ...