Leading and Trailing(数论/n^k的前三位)题解
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的前三位)题解的更多相关文章
- E - Leading and Trailing 求n^k得前三位数字以及后三位数字,保证一定至少存在六位。
/** 题目:E - Leading and Trailing 链接:https://vjudge.net/contest/154246#problem/E 题意:求n^k得前三位数字以及后三位数字, ...
- 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 ...
- LightOJ 1282 Leading and Trailing 数论
题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...
- Leading and Trailing LightOJ - 1282 (取数的前三位和后三位)
题意: 求n的k次方的前三位 和 后三位 ...刚开始用 Java的大数写的...果然超时... 好吧 这题用快速幂取模求后三位 然后用一个技巧求前三位 ...orz... 任何一个数n均可以表示 ...
- UTF-8格式txt文件读取字节前三位问题
今天试着读取一份UTF-8格式的txt文件,内容如下 12345 但是每次读取之后转为String类型,输出字符串长度总是为6,并且第一位打印在控制台后不占任何空间. 经过debug查看字节码后发现, ...
- UVA - 11029 输出前三位
题意:给定\(a\)和\(n\),输出\(a^n\)的前三位和后三位 后三位快速幂 \(log_{10}(a^n)=n*log_{10}(a)=n*log_{10}(x*y),y<10,x mo ...
- Java生成前三位是字母循环的字典
title: Java生成前三位是字母循环的字典 date: 2018-08-17 18:52:22 tags: Java --- 最近要破解一个秘密,还好这个密码是有线索的,已知密码的前三位是三个字 ...
- Codeforces Round #524 (Div. 2)(前三题题解)
这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...
- Lyft Level 5 Challenge 2018 - Final Round (Open Div. 2) (前三题题解)
这场比赛好毒瘤哇,看第四题好像是中国人出的,怕不是dllxl出的. 第四道什么鬼,互动题不说,花了四十五分钟看懂题目,都想砸电脑了.然后发现不会,互动题从来没做过. 不过这次新号上蓝名了(我才不告诉你 ...
随机推荐
- 【mlflow】执行import mlflow 报错:ImportError: No module named 'pkg_resources'
命令行运行 python -c “import mlflow” 的时候报错: ImportError: No module named 'pkg_resources' 结果发现是因为本地有一个文件夹叫 ...
- Find a way--hdu2612
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 广搜题 注意:可能存在一个@两人都不能达到: 3 3 Y#@ .M# @.. #include ...
- gulp自动化部署:gulp发布express项目(二)
原文:https://my.oschina.net/songzhu/blog/610337 一.服务器准备 服务器ip地址为:172.16.70.174 1.安装 Node.js 参考:http:// ...
- requests的post请求:百度翻译
import json import requests class Trans(object): def __init__(self, juzi): self.juzi = juzi self.bas ...
- SqlServer 凭据
一.理解索引的结构 索引在数据库中的作用类似于目录在书籍中的作用,用来提高查找信息的速度.使用索引查找数据,无需对整表进行扫描,可以快速找到所需数据.微软的SQL SERVER提供了两种索引:聚集索引 ...
- HPC高性能计算知识: 异构并行计算
版权声明:很多其它内容,请关注[架构师技术联盟]公众号 https://blog.csdn.net/BtB5e6Nsu1g511Eg5XEg/article/details/80059122 当摩尔定 ...
- CentOS工作内容(四)主机禁ping
CentOS工作内容(四)主机禁ping 用到的快捷键 tab 自动补齐(有不知道的吗) ctrl+a 移动到当前行的开头(a ahead) ctrl+u 删除(剪切)此处至开始所有内容 vim 末行 ...
- 万恶之源 - Python运算符与编码
格式化输出 现在有个需要我们录入我们身边好友的信息,格式如下: ------------ info of Alex Li ---------- Name : Alex Li Age : 22 job ...
- 高性能mysql 第1,2,3章。
一: 第一章 1:使用事务 start transaction; select * from t1; commit; 2:查看事务状态 mysql> show variables like 'a ...
- Py之pandas:dataframe学习【转载】
转自:https://www.tutorialspoint.com/python_pandas/python_pandas_dataframe.htm 1.数据框4特性 列是不同类型的数据元素. 每列 ...