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-快速幂+数学的更多相关文章

  1. LightOJ 1282 Leading and Trailing (快数幂 + 数学)

    http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Me ...

  2. LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)

    链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...

  3. 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 ...

  4. LightOJ 1282 Leading and Trailing (数学)

    题意:求 n^k 的前三位和后三位. 析:后三位,很简单就是快速幂,然后取模1000,注意要补0不全的话,对于前三位,先取10的对数,然后整数部分就是10000....,不用要,只要小数部分就好,然后 ...

  5. LightOj 1282 Leading and Trailing

    求n^k的前三位数字和后三位数字. 范围: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107). 前三位: 设 n^k = x ---> lg(n^k)=lg(x) - ...

  6. LightOJ 1282 Leading and Trailing 数论

    题目大意:求n^k的前三位数 和 后三位数. 题目思路:后三位数直接用快速幂取模就行了,前三位则有些小技巧: 对任意正数都有n=10^T(T可为小数),设T=x+y,则n=10^(x+y)=10^x* ...

  7. LightOJ - 1282 Leading and Trailing (数论)

    题意:求nk的前三位和后三位. 分析: 1.后三位快速幂取模,注意不足三位补前导零. 补前导零:假如nk为1234005,快速幂取模后,得到的数是5,因此输出要补前导零. 2.前三位: 令n=10a, ...

  8. 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 ...

  9. 1282 - Leading and Trailing ---LightOj1282(快速幂 + 数学)

    http://lightoj.com/volume_showproblem.php?problem=1282 题目大意: 求n的k次方的前三位和后三位数然后输出 后三位是用快速幂做的,我刚开始还是不会 ...

  10. LightOJ 1070 Algebraic Problem:矩阵快速幂 + 数学推导

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 题意: 给你a+b和ab的值,给定一个n,让你求a^n + b^n的值(MOD ...

随机推荐

  1. linux IPC 消息队列

    消息队列函数原型 在建立IPC通讯时(如消息队列,共享内存)必须建立一个ID值.通常情况下,这个ID值由ftok函数得到 #inlcude <sys/types.h> #include & ...

  2. 执行 systemctl start firewalld 命令后出现Failed to start firewalld.service: Unit is masked

    firewalld服务被锁定,不能添加对应端口 执行命令,即可实现取消服务的锁定 # systemctl unmask firewalld 下次需要锁定该服务时执行 # systemctl mask ...

  3. go解析markdown转成html

    一.代码 package main import ( "fmt" "github.com/microcosm-cc/bluemonday" "gith ...

  4. STM32嵌入式开发学习笔记(一)

    本文中,笔者将介绍使用嵌入式开发工具Keil uVision5,使用C语言,对微处理器STM32F103C8进行嵌入式开发. 开发使用C语言,首先需要新建一个C语言文件,将其设为主函数的入口,因此,将 ...

  5. springMVC整合swagger(亲自试验完全可用)

    swagger是什么: [plain] view plain copy Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一 ...

  6. Dubbo入门到精通学习笔记(四):持续集成管理平台之Maven私有库和本地库的安装与配置

    文章目录 介绍 Maven私有库和本地库的安装与配置 Nexus安装 Nexus 配置(登录后) 介绍 如果构建的Maven项目本地仓库没有对应的依赖包,那么就会去Nexus私服去下载, 那么如果Ne ...

  7. 使用target属性跳转到指定位置

    先上代码,使用frameset将网页分割为三个窗口,上,左和右. 1 <html> 2 <head> 3 <title>Main</title> 4 & ...

  8. 从内部重启python脚本

    我有一个基于python的GTK应用程序,可以加载几个模块.它是从(linux)终端运行的,如下所示: ./myscript.py --some-flag setting 用户可以从程序中下载(使用G ...

  9. 注册页面-使用form模块搭建

    基于Django的form模块,快速的搭建注册页面,每个限制条件,都放在form模块里面,不单独对每一项编写标签,使用模版的 for 循环来渲染. 首先设置form模块 在blogs模块下创建一个bl ...

  10. 44-Ubuntu-用户管理-09-chmod的数字表示法介绍

    chmod 修改文件和目录权限 chmod在设置权限时,可以简单地使用三个数字分别对应拥有者/组和其他用户的权限. 注意: chmod直接修改文件|目录的'读|写|执行'权限,但是不能精确到拥有者|组 ...