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出的. 第四道什么鬼,互动题不说,花了四十五分钟看懂题目,都想砸电脑了.然后发现不会,互动题从来没做过. 不过这次新号上蓝名了(我才不告诉你 ...
随机推荐
- vue中给请求到的数据对象加属性问题
今天发现了个很奇怪的问题,我在做一个:点击列表 使点中的列表项变色的功能,而且是多个大列表项,在每个大列表项里点击切换列表项的时候不影响其他大列表项的选项. 解决思路,因为这些大列表项是请求到的数据 ...
- vueawesomeswiper自定义 导航点
1,pagination的配置 pagination: { el: '.swiper-paginationfull', // type:'bullets', // bulletElement : 's ...
- CF1044B Intersecting Subtrees 构造+树论
正解:构造 解题报告: 传送门 又是一道交互题!爱了爱了! 这题真的,极妙!非常神仙!就非常非常思维题! 直接说解法了吼 说起来实在是简单鸭 就是先问一个对方的联通块中的一个点在我这儿的编号,记为x ...
- thinkphp 如何查询数据库
在控制器中使用M(); 数据表: 一.查询方式 1.字符串作为条件 2.使用索引数组作为条件查询条件 //可以更改逻辑关系 $condition['_logic'] = 'OR'; 二.表达式查询 三 ...
- 用Servlet获取表单数据
用Servlet获取表单数据 在webroot下新建userRegist2.jsp 代码如下: <%@ page contentType="text/html;charset=gb23 ...
- 微信小程序----团购或秒杀的批量倒计时实现
效果图 实现思路微信小程序实现倒计时,可以将倒计时的时间进行每一秒的计算和渲染! JS模拟商品列表数据 goodsList:在 onLoad 周期函数中对活动结束时间进行提取:建立时间格式化函数 ti ...
- canvas实现验证码功能
我们在做一些后台系统登录功能的时候,一般都会用到验证码,最多的就是后台生成的验证码图片返回给前端的.也可以不调用后端接口,前端使用canvas直接生成验证码. 由于功能过于简单,不需要多少代码和文字说 ...
- csv到mysql数据库如何分割
这两天修改一个取XML文件存入到CSV,然后再存入到mysql的bug,bug是XML文件里面有个name字段,存入CSV文件里面的时候我们用“|”,来分割字段.但是name里面有时候也有 ...
- C#方法参数传递-同时使用ref和out关键字
在方法参数传递中,可以同时使用ref和out关键字,但是要注意ref和out参数传递的不同. using System;class Program{static void Main(){ Pro ...
- spring mvc interceptors
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:e ...