Leading and Trailing LightOJ - 1282 (取数的前三位和后三位)

题意:

求n的k次方的前三位 和 后三位 。。。刚开始用 Java的大数写的。。。果然超时。。。

好吧  这题用快速幂取模求后三位  然后用一个技巧求前三位 。。。orz。。。

任何一个数n均可以表示为10a, 其中 a 可以为小数

那么nk 可以表示为10ak  , 令ak == x + y  (其中x为整数 y为小数)  所以 ak - x == y

fmod(x,1)可以返回x的小数部分 所以y = fmod(ak,1)

/*由于x是整数,那么很明显他是用来指定位数的,因为10x肯定是一个10, 100, 1000...之类的数字,也就是说10y才是这个数字的有效部分,我们只要求出10y,然后乘上100最终结果就是我们想要的。

因为n = 10a  所以 a = log10(n),10y就是小数部分,我们用函数fmod(x, 1),返回x的小数部分,然后乘上100即可*/

fmod返回的是y的值,所以必须计算10y才是真实值,所以直接使用102 * 10y 即pow(10, 2 + y);

因为10y是整数一位的小数  所以要乘100

 

代码如下:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 1000
#define LL long long
#define ULL unsigned long long
#define maxn 100009
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int LL_INF = 0x7fffffffffffffff,INF = 0x3f3f3f3f;
LL qpow(LL a,LL b)
{
    LL res = 1;
    while(b)
    {
        if(b & 1) res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

int main()
{
    int T;
    int cnt = 0;
    cin>> T;
    while(T--)
    {
        int n, k;
        cin>> n >> k;

        int res = pow(10, 2 + fmod(k * log10(n), 1));

        printf("Case %d: %d %03d\n",++cnt,res,qpow(n,k));

    }


    return 0;
}

 

转载于:https://www.cnblogs.com/WTSRUVF/p/9182565.html

lightoj1282 - Leading and Trailing(水题) 水题 末位模快速幂 首位瞎搞 题目链接 http://lightoj.com/volume_showproblem.php?problem=1282#include <stdio.h> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> using nam 阅读详情

相关推荐

LightOJ 1282 Leading and Trailing

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/E Leading and Trailing Description You are given two integers: n and k, your task is to find the most signi

大太阳 1351

LightOJ - 1282 Leading and Trailing(快速幂+)

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1282 题目大意:给出n^k的前三位三位字 题目思路:后三位简单,直接快速幂%100即可,记得如果不够3位要补0,前三位两种做法 做法一:好像大部分都是采用方法一,方法是设m满足等式,xy分别表示m的整部分部分...

smilestruggler_Fly To Higher 300

LightOJ1282Leading and Trailing论)

题面Vjudge题解这题。。真的就是搞笑的 第二问,直接输出快速幂mod 1000mod \ 1000的值,要补前导零 第一问。。。就是搞笑的 依旧是快速幂 但是用double来算 每次中间值只要大于1000 直接除得小于1000就行了 不会就看代码把。。 这题就是搞笑的。。。#include<iostream> #include<cstdio> #include<cstdlib>

小蒟蒻yyb的博客 264

LightOJ 1282 Leading and Trailing

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 nkn^knk. Input Input starts wi...

旺崽的博客 2442

LightOJ 1282 - Leading and Trailing (求n^k的前三位三位

1282 - Leading and Trailing   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are given two integers: n and k, your

滴水穿石,慢慢积累,总会有成果 1704

Leading and TrailingLightOJ - 1282,求n^k 的前三位 三位

一.题目链接: LightOJ-1282 二.题目大意: 给出 n k,求的前三位 三位. 三.分析: 后三位用快速幂求即可. 前三位求法: 首先复习一下科学计法: ( 0 < A < 10) 一定可以表示成的形式 ( x 为整,y 为小 ) 即==,== A 的前三位 == A100 对等...

The___Flash的博客 256

Leading and Trailing__LightOJ 1282(求n^k的前三位三位)

轻触打开原网站 LightOJ_1282 题目要求: You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of n^k. Input Input starts with an integer...

From now on...的Blogs 188

LightOJ - 1282 Leading and Trailing

LightOJ - 1282 Leading and Trailing 传送门:LightOJ - 1282 题意 给你两个n,kn,kn,k,让你求nknkn^k的前三位三位各是多少。 1≤n&amp;lt;231,1≤k≤1071≤n&amp;lt;231,1≤k≤1071 \le n \lt 2^{31},1 \le k \le 10^7 题解 后三位很好想,直接快速幂搞一下,膜100...

fnoi11awyfeng的博客 344

LightOJ 1282 - Leading and Trailing (求n^k的前三位三位

1282 - Leading and Trailing PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are given two integers: n and k, your taskis to find the most significant thre...

pxlsdz的博客 269

Leading and Trailing LightOJ - 1282(学技巧,求前三位与后三位 )

题目链接: C - Leading and Trailing LightOJ - 1282 题意: 给你T组据,每组一个n,一个k,范围是:T (≤ 1000), n (2 ≤ n &lt; 231) and k (1 ≤ k ≤ 107). 问:n^k 的 前三位与n^k的后三位是多少? Sample Input 5 123456 1 123456 2 2 31 2 ...

ltrbless的博客 269

Leading and Trailing-求n^k的前三位三位-LightOJ1282

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 (≤ 1000), denoting the numbe...

Nothing_but_Fight的博客 261

Lightoj 1282 (Leading and Trailing)

Leading and Trailing Description 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 st

渣渣眼里没有水题 401

LightOJ - 1282 Leading and Trailing前三位三位

You are given two integers:nandk, your task is to find the most significant three digits, and least significant three digits ofnk. Input Input starts with an integerT (≤ 1000), denoting the num...

XYBDX的博客 301

Leading and Trailing LightOJ - 1282 题解

LightOJ - 1282 Leading and Trailing 题解 纵有疾风起 题目大意 题意:给你一个n,让你求这个的k次方的前三位最后三位。 \(2<=n<2^{31}\),\(1<=k<10^{7}\) 并且\(n^{k}\)至少有6位 解题思路 这个题目需要解决两个问题 输出\(n^{k}\)前三位 输出\(n^{k}\)的...

weixin_30347009的博客 132

(浮点的幂) - Leading and Trailing - LightOJ 1282

(浮点的幂) - Leading and Trailing - LightOJ 1282 题意: 给定两个n,k,求nk的前三位最后三位。给定两个n,k,求n^k的前三位最后三位。给定两个n,k,求nk的前三位最后三位。 输入: T组测试据,T组测试据,T组测试据, 每组包括两个正整nk。每组包括两个正整nk。每组包括两个正整nk。 输出: nk的前三位最后三位。n^k的前三位最后三位。nk的前三位最后三位。 Sample Input 5 123456 1 1234

njuptACMcxk的博客 254

lightoj 1282 - Leading and Trailing学】

1282 - Leading and Trailing     PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are given two integers: n and k, your task is to

世界很大 690

LightOJ - 1282 Leading and Trailing

n^k的前三位

a free man 515
上一篇: Sigma Function LightOJ - 1336 (约数和为奇数)
下一篇: Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)
babing2770
博客等级 码龄10年 0粉丝 161原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值