【数论】A - Leading and Trailing

A - Leading and Trailing
给定两个数n,k 求n^k的前三位和最后三位
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.

Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).
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:
5
123456 1
123456 2
2 31
2 32
29 8751919
Sample Output:
Case 1: 123 456
Case 2: 152 936
Case 3: 214 648
Case 4: 429 296
Case 5: 665 669
题意很简单,后三位直接快速幂mod1000,前三位取对数,用fmod,
为了求nk的前三位,把n写成10a(a为小数)的形式。因此原式=10(ak).而ak可以写成x+y,其中x为ak的整数部分,y为ak的小数部分,所以x决定了位数,y决定了值,因此求出y即可。而n=10a => a=log10(n),fmod(x,1)可以求出x的小数部分,因此用fmod(ak,1)即可求出y

  • -原型:extern float fmod(float x, float y)
    用法:#include <math.h>
    功能:计算x/y的余数
    说明:返回x-n*y,符号同y。n=[x/y] (向离开零的方向取整),如:fmod(f,(int)f)即可得到小数点后的部分
    --------------------代码:
#include <iostream>
#include <cstring>
#include<cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <ctime>
#include <cstring>
#include <cmath>
#include <stack>
#include <set>

#define INF 0x3f3f3f3f
#define rep(i, n) for(ll i=0;i<n;i++)
#define per(i, n) for(int i=n;i>=0;i--)
#define rep2(i, n) for(ll i=1;i<=n;i++)
#define pb(x) push_back(x)
#define clint(x, n) memset(x,n,sizeof(x))
#define mp make_pair
#define fi first
#define se second
#define IO std::ios::sync_with_stdio(false)
#define ll long long
#define ull unsigned long long
#define ud long double
#define pii pair<int,int>
using namespace std;
const int maxn = 2e5+10;
const int inf = 1000000 + 10;
const int mod =1000;
const int MAX = 123123;
ll qpow(ll base,ll n)
{
    base%=mod;
    ll ans=1LL;
    while(n>0){
        if(n&1)
            ans=(ans*base)%mod;
        base=(base*base)%mod;
        n>>=1;
    }
    return ans%mod;
}
int main() {
    int t,cnt=0;
    scanf("%d",&t);
    while(t--){
        ll n,k;
        scanf("%lld%lld",&n,&k);
        int strat=(int)pow(10.0,2.0+fmod(k*log10(n*1.0),1.0));//前三位
        int end=(int)qpow(n,k);//后三位
        printf("Case %d: %03d %03lld\n",++cnt,strat,end);
    }
    return 0;
}
全部评论

相关推荐

04-29 22:35
门头沟学院 Java
牛友说改了名字能收到offer:旧图新发查看图片
点赞 评论 收藏
分享
05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务