UVa 11029 Leading and Trailing

题目要求输出N的K次方的前三位和后三位。后三位的解法不用多说了,用二分法快速去模即可。关键是前三位怎么求?题目中说N能用32位带符号整数表示,K最大是10的六次方。因此N^K的解ans最多不过10^(9*10^6),因此我们完全可以用以十为底的对数x+y表示,其中x表示对数的整数部分,y表示对数的小数部分。显然,ans的具体数字是由10^y来表示的,而x只是用来将小数以为成整数而已。并且可以确定的是,10^y必小于10且大于1,因为大于10的话,y就大于1了,而小于1的话,y就小于0了显然不可能。

因此前三位只要将10^y*100取整数部分即可。最后,还要注意的一点是,后三位取模后可能不是一个三位数,那么需要在前面补上相应个数的0。

#include <cstdio>
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
typedef long long LL;

LL T,k,n;
int leading,trailing;

LL g_mod(LL,LL);

int main()
{
    //freopen("data.txt","r",stdin);
    cin>>T;

    while(T--){
        cin>>n>>k;
        trailing=g_mod((LL)n%1000,k);//二分法取模

        int zero;
        if(!(trailing/10)){zero=2;}//确定前缀零的个数
        else if(!(trailing/100)){zero=1;}
        else {zero=0;}

        double x=k*(log(n)/log(10));
        x-=(int)x;//获取对数的小数部分
        leading=(int)(pow(10,x)*100);//取得末尾三位
        cout<<leading<<"..."<<string(zero,'0')<<trailing<<endl;
    }

    return 0;
}

LL g_mod(LL n,LL k)
{
    if(k==1) return n;
    LL ans=g_mod(n,k/2)%1000;
    ans=(ans*ans)%1000;
    if(k%2) ans=ans*n%1000;

    return ans;
}
时间: 2024-12-08 11:56:14

UVa 11029 Leading and Trailing的相关文章

UVA 11029 Leading and Trailing(大数n^k的前x位高精度问题)(好题)

Problem C Leading and Trailing Time limit: 2 seconds   Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double da

UVA - 11029 - Leading and Trailing (快速幂+公式变形)

题目传送:UVA - 11029 思路:后三位可以直接快速幂取模,然后前三位可以有两种做法,一个是利用double,一个是利用公式法,具体看代码吧 注意,后三位不足三位要补0,即用%03d AC代码①: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #incl

Uva 11029 Leading and Trailing (求n^k前3位和后3位)

题意:给你 n 和 k ,让你求 n^k 的前三位和后三位 思路:后三位很简单,直接快速幂就好,重点在于如何求前三位,注意前导0 资料:求n^k的前m位 博客连接地址 代码: #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> #define ll long long using namespace std; ll qmod(ll a,ll b,ll mod)

UVA Leading and Trailing 11029【数学+快速幂】

11029 - Leading and Trailing Time limit: 3.000 seconds Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double da

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 three digits, and least significant three digits of nk. Input Input starts with an integer T (≤ 1000), denoting the number of test cases. Each case st

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

http://lightoj.com/volume_showproblem.php?problem=1282 Leading and Trailing Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1282 Description You are given two integers: n and k, your task is t

[题解]UVA11029 Leading and Trailing

链接:http://vjudge.net/problem/viewProblem.action?id=19597 描述:求n^k的前三位数字和后三位数字 思路:题目要解决两个问题.后三位数字可以一边求高次幂一边取模,下面给出求前三位数字的方法. n^k = (10^lg n)^k = 10^(k*lg n) 为了描述方便,令X=n^k .则 lg X 的整数部分表示X有多少位.设整数部分为zs,小数部分为xs,则X=(10^zs)*(10^xs) . (10^zs)的形式就是100……,跟X的位

UVA11029 Leading and Trailing【快速模幂+数学】

Apart from the novice programmers, all others know that you can't exactly represent numbers raised to some high power. For example, the C function pow(125456, 455) can be represented in double data type format, but you won't get all the digits of the

Leading and Trailing (数论)

Leading and Trailing https://vjudge.net/contest/288520#problem/E 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), den