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. Shiro学习(9)JSP标签

    Shiro提供了JSTL标签用于在JSP/GSP页面进行权限控制,如根据登录用户显示相应的页面按钮. 导入标签库 Java代码   <%@taglib prefix="shiro&qu ...

  2. 【LeetCode 2】两数相加

    描述 [题解] 模拟高精度的加法. 用x来记录前面的进位就好. [代码] /** * Definition for singly-linked list. * struct ListNode { * ...

  3. QtConcurrent::run() 的使用

    QFuture<T>run(constClass *object,T(Class::*fn)(Param1,Param2,Param3,Param4,Param5)const,constA ...

  4. golang 读取 ini配置信息

      package main //BY: 29295842@qq.com//这个有一定问题   如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost   ...

  5. git 问题整理 fetch -p 修剪远程分支

    修剪远程分支 问题描述 前提:假设现在远程有三个分支master.testA.testB 问题:使用命令 git fetch origin 获取到远程分支,然后使用命令 git branch -r 来 ...

  6. ALSA 更改默认声卡

    用 aplay -l 看一下各声卡的配置信息 创建 /etc/asound.conf,根据 aplay -l  的输入,选择需要的声卡信息,按如下格式添入,即可更改默认声卡 defaults.pcm. ...

  7. CM 安装cdh 版本及出现问题

    添加节点: 安装完cdh 后jps出现:process information unavailable 解决办法:

  8. scala 基础笔记

    view bound:必须传入一个隐式转换函数 class [T <% Ordered [T]] content bound:必须传入一个隐式值 class [T : Ordering] !异步 ...

  9. lca 倍增模版

    ; void dfs(int u,int fa){ d[u]=d[fa]+; p[u][]=fa; ;i<POW;i++) p[u][i]=p[p[u][i-]][i-]; int sz=edg ...

  10. sanic中间件和监听器

    一:中间件 中间件是服务器在请求之前或之后执行的功能,他们可以用来修改修改用户定义处理函数的请求或相应. Sanic提供两种类型的中间件:请求和响应. 两者都是使用@app.middleware装饰器 ...