博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leading and Trailing(数论/n^k的前三位)题解
阅读量:6947 次
发布时间:2019-06-27

本文共 1625 字,大约阅读时间需要 5 分钟。

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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define ll long longconst int N=10000003; //18const 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;}

转载于:https://www.cnblogs.com/KirinSB/p/9409120.html

你可能感兴趣的文章
演示:外部全局地址与外部局部地址的使用案例
查看>>
Exchange Server 2013 公网发布疑难解答
查看>>
Oracle 12c dataguard云上挖坑记--为某机场贵宾业务部署oracle 12c到云端
查看>>
前端开发在不久的将来定会成为主导
查看>>
jQuery内ready与load事件的区别
查看>>
[笔记].关于Stratix III使用非易失加密后,无法正常配置启动的问题探讨
查看>>
一个通用的单元测试框架的思考和设计03-实现篇-核心类源码
查看>>
载入史册 改变IT安全历程的十大里程碑
查看>>
UVA 624 CD
查看>>
Windows phone 7: DataBinding and UI Refresh系列教程
查看>>
矩阵快速幂 学习笔记
查看>>
linux iconv 批量转码
查看>>
使用MongoDB的GridFS保存用户文件的折腾日记
查看>>
ios开发工程师笔试基础题
查看>>
基于Struts构建新闻发布系统
查看>>
基于Struts实现用户登录和注册模块
查看>>
C++ getline函数的使用
查看>>
SQL Server删除重复行的6个方法
查看>>
Mysql 临时表的创建和删除
查看>>
db file scattered read等待事件
查看>>