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学习(21)授予身份及切换身份
在一些场景中,比如某个领导因为一些原因不能进行登录网站进行一些操作,他想把他网站上的工作委托给他的秘书,但是他不想把帐号/密码告诉他秘书,只是想把工作委托给他:此时和我们可以使用Shiro的RunAs ...
- 使用ionic来build安卓apk时,报CordovaError: Requirements check failed for JDK 1.8 or greater
本地配置了JDK和jre的本地环境变量之后,在命令行中运行Java.javac等都能正常输出,但是在使用ionic cordova build android 来打包ionic的程序时,会提示 Cor ...
- sublime text3 nodejs控制台输出结果中文乱码
在sublime text3安装完nodejs的插件后,运行console.log("你好"),发现控制台出现中文乱码,解决办法:Preferences-> Browser ...
- (转)OpenFire源码学习之二十七:Smack源码解析
转:http://blog.csdn.net/huwenfeng_2011/article/details/43484199 Smack Smack是一个用于和XMPP服务器通信的类库,由此可以实现即 ...
- 听说这个FFT跑得巨jb快
#pragma GCC target ("avx2") #include <immintrin.h> #include<bits/stdc++.h> usi ...
- samba环境部署
安装samba yum install -y samba samba-client vim /etc/samba/smb.conf (共享一个目录,任何人都可以访问但不能写修改) workgroup ...
- Jeecg 切换默认首页方法
类名: org.jeecgframework.core.enums.SysThemesEnum
- 结合Intel Manual和libdasm学习汇编指令
参考:http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html h ...
- Codeforces 340B - Maximal Area Quadrilateral (计算几何)
Codeforces Round #198 (Div. 2) 题目链接:Maximal Area Quadrilateral Iahub has drawn a set of \(n\) points ...
- 【Java多线程系列二】Thread类的方法
Thread实现Runnable接口并实现了大量实用的方法. /* * 此方法释放CPU,但并不释放已获得的锁,其它就绪的线程将可能得到执行机会,它自己也有可能再次得到执行机会 */ public s ...