lightoj 1282 Leading and Trailing
来源:互联网 发布:ubuntu设置ip自动获取 编辑:程序博客网 时间:2024/05/16 15:33
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
Output for Sample Input
5
123456 1
123456 2
2 31
2 32
29 8751919
Case 1: 123 456
Case 2: 152 936
Case 3: 214 648
Case 4: 429 296
Case 5: 665 669
题目大意:输出前三位和后三位;
题解:后三位这里如果是2的话,输出002 ; 如果是22输出022;后三位用快速幂取模然后“%03lld”输出;
前三位的话推公式:
如果有 x = n^k = k*(log10(n*1.0));
p1 = k*(log10(n*1.0)) - (int) k*(log10(n*1.0)); 这里是小数部分;
y =(int) pow(10,p1+2.0); 因为要求的是前三位; 即可;
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#define ll long long using namespace std ;ll quick(ll a , ll b ,ll mod){ll res = 1 ;a = a % mod ;while(b){if(b%2) res = res * a % mod ;b/=2 ;a = a * a % mod ;}return res ;}double fun(int n , int k ){double most ; most = k*log10(n*1.0) ;most -=(int)most;most=pow(10,2.0+most);return most ;}int main(){int t ;cin>>t;for(int cas = 1 ; cas <= t ; cas++){ll a , b ;cin>>a>>b;ll judge1 = quick(a,b,1000);ll judge2 = fun(a,b);printf("Case %d: %lld %03lld\n",cas,judge2,judge1);}return 0 ;}
- lightOJ 1282 Leading and Trailing
- LightOJ 1282 Leading and Trailing
- LightOJ 1282 Leading and Trailing
- lightoj 1282 Leading and Trailing
- Lightoj 1282 (Leading and Trailing)
- 【Lightoj 1282 Leading and Trailing】
- LightOJ 1282 Leading and Trailing
- LightOJ 1282 Leading and Trailing
- LightOJ 1282 Leading and Trailing
- LightOJ 1282 Leading and Trailing
- lightoj 1282 Leading and Trailing
- LightOJ 1282 Leading and Trailing
- lightoj Leading and Trailing
- Leading and Trailing LightOJ
- Leading and Trailing LightOJ
- Leading and Trailing LightOJ
- Leading and Trailing LightOJ
- lightoj 1282 - Leading and Trailing 【数学】
- 自定义Toast
- 准备软考
- 字符串截取
- Could not autowire field: private com.dounion.ygyc.biz.business.BizArchivesBusiness com.dounion.ygyc
- ActiveMQ消息传送机制以及ACK机制详解
- lightoj 1282 Leading and Trailing
- GNU GCC是干什么的,是不是开发环境,还是一种术语
- php 实现(非递归,栈模拟)二叉树的先序、中序、后序
- 遇到的问题----java--myeclipse部署时An internal error occurred NullPointException错误
- NYOJ 91 阶乘之和 贪心算法
- boost::asio::ip::tcp实现网络通信的小例子
- linux下的文件系统选型
- Android开发之将Edittext输入弹出的软键盘设置搜索确定键。详细代码,已验证可用。
- red5源码分析---5