LightOJ-1282-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 (≤ ), denoting the number of test cases. Each case starts with a line containing two integers: n ( ≤ n < ) and k ( ≤ k ≤ ). 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 Sample Output
Case :
Case :
Case :
Case :
Case :
题意:
求n^k的前三位leading和后三位treiling。
一开始英文是没看懂的,
第二次做是知道用什么方法,但也是个大概,因为很多小细节需要注意,强制转换和控制格式。
fmod函数的具体用法:
https://www.runoob.com/cprogramming/c-function-fmod.html
返回double型 fmod(double,int);
思路:
前三位:运用对数(这一部分我好像没有学好)
后三位:快速幂取余
求一个数的几次方的前三位有一个公式=n^k/(10^(t-3));
fmod(double,int)是一个函数,求一个数的小数部分;
由于任意一个数都可以写成10的几次方,只不过这个几次方可能是个小数;
所以小数部分就是决定前几位的数是几的关键,然后再用pow()函数。
这一题求快速幂的时候不能传入int,因为在第二组数据上的后三位会造成数据溢出int变成负数。
但是我也不知道为什么主函数传入int,在调用的函数中定义为ll是可以的???这是一个问题。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
typedef long long ll;
using namespace std; ll mod_pow(ll x,ll n,ll mod)
{
ll res=;
while(n>)
{
if(n&)
res=res*x%mod;
x=x*x%mod;
n>>=;
}
return res;
} int main()
{
int t,n,k;
int tt=;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d %d",&n,&k);
double qq=pow(*1.0,fmod(k*1.0*(log10(n*1.0)),));////前三位
int hh=mod_pow(n,k,);//后三位
printf("Case %d: %03d %03d\n",tt++,(int)(qq*),hh);
}
}
return ;
}
LightOJ-1282-Leading and Trailing-快速幂+数学的更多相关文章
- LightOJ 1282 Leading and Trailing (快数幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS Me ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- UVA 11029 || Lightoj 1282 Leading and Trailing 数学
Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...
- LightOJ 1282 Leading and Trailing (数学)
题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后 ...
- LightOj 1282 Leading and Trailing
求n^k的前三位数字和后三位数字. 范围: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107). 前三位: 设 n^k = x ---> lg(n^k)=lg(x) - ...
- LightOJ 1282 Leading and Trailing 数论
题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...
- LightOJ - 1282 Leading and Trailing (数论)
题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a, ...
- 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 ...
- 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)
http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会 ...
- LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导
题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...
随机推荐
- bzoj1072题解
[解题思路] 状压DP.f[i][j][k]表示当前DP到第i位,模d意义下余数为j,状态为k的方案数.其中状态k表示每个数字还剩多少个没取,状态数最多210. 于是有递推式转移方程:f[i+1][( ...
- noip历年试题
noip2018 铺设道路 货币系统 赛道修建 一眼贪心.随便实现. 旅行 环套树枚举删除环上哪条边. 填数游戏 找规律,这谁会啊. 保卫王国 动态Dp,去问这位神仙. noip2017 小凯 ...
- Hbase的rowkey设计
HBase的rowKey设计技巧 1.设计宗旨与目标 主要目的就是针对特定的业务模型,按照rowKey进行预分区设计,使之后面加入的数据能够尽可能的分散于不同的rowKey中.比如复合RowKey. ...
- Mysql LOAD DATA读取客户端任意文件漏洞复现(原理分析)
环境搭建 怎么设置Mysql支持外联? use mysql; grant all privileges on *.* to root@'%' identified by '密码'; //授权语句 fl ...
- NX二次开发-使用MFC对话框不能用UF_UI_select等函数解决方法
VC/MFC调用UG Dialog要进入加锁状态 加锁 UF_UI_lock_ug_access ( UF_UI_FROM_CUSTOM ); 此处为UF_UI_select的函数 解锁 UF_UI_ ...
- python内置模块-random
print(random.randint(1,10)) 生成随机整数,下限必须小于上限print(random.randrange(1,10)) 生成随机整数,参数为([start],stop,[st ...
- 如何查看bug属于前端还是后端
1.F12下如何查看bug属于前端还是后端?前后端分离的项目,通过ajax向后端请求数据,如果后端返回的数据有问题,那么问题就是候选,如果返回的数据没有问题,但是展示结果异常那么问题一般就出在前端. ...
- Socket通信1.0
Socket通信1.0 服务器端: package page; import java.io.BufferedReader; import java.io.IOException; import ja ...
- 如何从ST官网下载STM32标准库
Frm:https://blog.csdn.net/k1ang/article/details/79645044
- 2019牛客多校第三场J-LRU management(map+双向链表)
LRU management 题目传送门 解题思路 用map索引对应地址,用双向链表维护序列. 代码如下 #include <bits/stdc++.h> #define INF 0x3f ...