Leading and Trailing(LightOJ - 1282)
题解:求一个数的次幂,然后输出前三位和后三位,后三位注意有前导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)的更多相关文章
- 【LightOJ1282】Leading and Trailing(数论)
[LightOJ1282]Leading and Trailing(数论) 题面 Vjudge 给定两个数n,k 求n^k的前三位和最后三位 题解 这题..真的就是搞笑的 第二问,直接输出快速幂\(m ...
- Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)
Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...
- Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】
Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...
- Goldbach`s Conjecture(LightOJ - 1259)【简单数论】【筛法】
Goldbach`s Conjecture(LightOJ - 1259)[简单数论][筛法] 标签: 入门讲座题解 数论 题目描述 Goldbach's conjecture is one of t ...
- Leading and Trailing (数论)
Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k ...
- Leading and Trailing(数论/n^k的前三位)题解
Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...
- LightOJ - 1282 Leading and Trailing (数论)
题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a, ...
- 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 ...
- (最长公共子序列+推导)Love Calculator (lightOJ 1013)
http://www.lightoj.com/volume_showproblem.php?problem=1013 Yes, you are developing a 'Love calcula ...
随机推荐
- Python 【Debug排除程序故障】
debug #排除程序故障 print()函数常和#号注释结合在一起用来debug 多行注释有两种快捷操作:1.在需要注释的多行代码块前后加一组三引号''' 2.选中代码后使用快捷键操作:Window ...
- 使用haystack实现django全文检索搜索引擎功能
前言 django是python语言的一个web框架,功能强大.配合一些插件可为web网站很方便地添加搜索功能. 搜索引擎使用whoosh,是一个纯python实现的全文搜索引擎,小巧简单. 中文搜索 ...
- intellij IDE 破解 简单 License server 法
http://idea.iteblog.com/key.php
- Flask框架入门
Flask-基本入门 简介 flask被称为微型框架,只提供了一个强健的核心,其他功能全部通过扩展库来实现:也就是说可以根据项目需要量身打造.他适合入门学习以及高手研究. 组成:WSGI.模板引擎(J ...
- 安卓开发之ArrayAdapter使用
package com.lidaochen.test; import android.support.v7.app.AppCompatActivity; import android.os.Bundl ...
- late_initcall 替换 module_init
今天在调试pwm驱动程序的时候,在__init函数中调用pwm_init后,则以太网不可用.pwm_init放在设备文件的open函数中,则系统正常运行. 这当中的区别就是硬件初始化函数pwm_ini ...
- CAFFE(FAQ.2):Ubuntu 配置caffe 框架之数据库读取,错误解决:ImportError: No module named leveldb解决办法
Z: 在安装了caffe框架后需要读取大量的数据进行学习训练.比如在MNIST识别训练中,一般直接读图片会比较耗时,我们一般将图片转存为数据库中.目前主流的数据库有以下两种选择: LevelDB Lm ...
- Systemd: Service File Examples
大多数Linux发行版使用 systemd作为系统服务管理工具. systemctl是systemd的主要命令,用于管理控制服务. 这篇文章中将介绍如何创建允许你使用systemctl命令的sysyt ...
- 洛谷 P2756 飞行员配对方案问题 (二分图/网络流,最佳匹配方案)
P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其 ...
- Almost Acyclic Graph CodeForces - 915D (思维+拓扑排序判环)
Almost Acyclic Graph CodeForces - 915D time limit per test 1 second memory limit per test 256 megaby ...