light_oj 1282 求n^k的前几位数和后几位数

light_oj 1282 求n^k的前几位数和后几位数

E - 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 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 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

题意:输出n^k的前三位和后三位。

思路:后三位直接快速幂取模,前三位化为科学计数法取对数推导:

n^k=a.bc*10^m ( m为n^k的位数,即m=(int)lg(n^k)=(int)(k*lgn) );

求对数:  k*lgn=lg(a.bc)+m

即 a.bc=10^(k*lgn-m)=10^(k*lgn-(int)(k*lgn));

abc=a.bc*100;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);
const int p=1000;

ll n,k;

ll qpow(ll n,ll k)
{
    ll res=1;
    while(k){
        if(k&1) res=(res%p)*(n%p)%p;
        n=(n%p)*(n%p)%p;
        k>>=1;
    }
    return res;
}

ll f(ll n)
{
    double x=k*log10(n)-(int)(k*log10(n));
    return pow(10,x)*100;
}

int main()
{
    int T;cin>>T;
    int tag=1;
    while(T--){
        cin>>n>>k;
        printf("Case %d: %lld %03lld\n",tag++,f(n),qpow(n,k));
    }
    return 0;
}

时间: 2024-08-08 13:25:31

light_oj 1282 求n^k的前几位数和后几位数的相关文章

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)

light_oj 1236 求最小公倍数( lcm(a,b) )等于n的数对 素因数分解

light_oj 1236 求最小公倍数( lcm(a,b) )等于n的数对  素因数分解 H - Pairs Forming LCM Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1236 Description Find the result of the following code: long long pairsFormL

2/1+3/2+5/3+8/5+13/8+…求出这个数列前20项的和

★有一个分数序列2/1+3/2+5/3+8/5+13/8+-求出这个数列前20项的和. #include<stdio.h> #include<stdlib.h> int main() { int i = 0; double x = 2.0, y = 1.0, z = 0.0; double sum = 0; for (i = 1; i <= 20; i++) { sum = sum+x / y; z = x; x = x + y; y = z; } printf("

light_oj 1138 求阶乘后导零的个数

light_oj 1138  求阶乘后导零的个数 N - Trailing Zeroes (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1138 Description You task is to find minimal natural number N, so that N! contains exactly Q 

树边,前向边,后向边,横叉边

转自http://www.gonglin91.com/dfs-graph-edge/ 树边,前向边,后向边,横叉边,应该说,不是一个图本身有的概念,应该是图进行DFS时才有的概念.图进行DFS会得到一棵DFS树(森林),在这个树上才有了这些概念.对图进行DFS,可以从任意的顶点开始,遍历的方式也是多样的,所以不同的遍历会得到不同的DFS树,进而产生不同的树边,前向边,后向边,横叉边.所以这4种边,是一个相对的概念.在图的遍历中,往往设置了一个标记数组vis的bool值来记录顶点是否被访问过.但有

生成订单号 、生成优惠券号 前四位大写字母 后六位数字

// 生成订单号 public static String setRandomChar() { String str = ""; for (int i = 0; i < 10; i++) { int ch = (int) (10 * (Math.random())); str = str + ch; } return str; } // 生成优惠券号 前四位大写字母 后六位数字 public static String genCouponCode() { String str =

c语言代码编程题汇总:升序,奇数在前,偶数在后

升序,奇数在前,偶数在后 自己的代码: 1 /* 2 2017年3月14日12:52:39 3 功能:升序,奇数在前,偶数在后 4 */ 5 #include "stdio.h" 6 int main () 7 { 8 int j = 0, k = 0; 9 int a[10]; 10 int b[10]; 11 int c[10]; 12 int *pa = a; 13 14 printf("please input 10 number: \n"); 15 for

?快速删除大文件的前几行或后几行及快速获取大文件的n到m行数据

快速删除大文件的前几行或后几行 http://stackoverflow.com/questions/17330188/remove-first-n-lines-of-a-file-in-place-in-unix-command-line 快速获取大文件的n到m行数据 http://unix.stackexchange.com/questions/47407/cat-line-x-to-line-y-on-a-huge-file

php while循环 指定显示内容 例如不想显示前10条和后10条

<?php //查询信息总的条数 $db_num = query_num("表","where 1=1"); //每页显示的条数 $page_size=200; //总条目数 $nums=$db_num; //每次显示的页数 $sub_pages=5; if(!$pageCurrent) $pageCurrent=1; $page_num=$pageCurrent-1; $page_num=$page_num*$page_size; $list_sql = m