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 ...
随机推荐
- Shiro学习(6)Realm整合
6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系 即用户-角色之间 ...
- luoguP2148 [SDOI2009]E&D [sg函数][组合游戏]
题目描述 小E 与小W 进行一项名为“E&D”游戏. 游戏的规则如下: 桌子上有2n 堆石子,编号为1..2n.其中,为了方便起见,我们将第2k-1 堆与第2k 堆 (1 ≤ k ≤ n)视为 ...
- xlwings结合dataframe数据的写入
一.代码 import xlwings as xw import pandas as pd xl_path=r'***' df_path=r'***' df=pd.read_excel(df_path ...
- 关于Kerberos协议流程的总结
Kerberos协议工作原理分析 这里面借用一下师傅们的图来说明一下  Kerberos协议的流程大致如下(假设A要获取对Server B的访问权限) 第一步(KRB_AS_REQ) 这一步客户 ...
- vue 复习篇. 注册全局组件,和 组件库
初篇 ============================================================== 1. 编写loading组件(components/Loading/ ...
- springboot使用自带连接池连接postgre
Application配置spring.datasource.url=jdbc:postgresql://***:5432/postgresspring.datasource.username=pos ...
- Java异常关闭资源的两种方式
try-catch-finally 常用,在异常关闭时应判断流是否为空 public class CloseableUtils { public static void closeable(Close ...
- Linux两台机器简历信任
cd ~/.ssh ssh-keygen -t rsa scp ./id_rsa.pub root@192.168.1.1:/root/.ssh/authorized_keys
- 拾遗:git pull 与 push 远程分支与本地分支顺序识别问题
最后放置的都是数据最终到达的仓库分支名称 对于pull来说,是拉到本地,所以本地仓库分支名称写在最后 git pull [--force] [remote repo]:[my repo] 对于push ...
- Red Hat Enterprise Linux 7.7 使用最小化安装后,怎么安装桌面的解决方法
准备工具: xshell6,xftp6,到官网(https://www.netsarang.com/zh/downloading/)进行下载,教育版的,个人使用 虚拟机安装教程百度即可,安装时有两个重 ...