uva11029 - Leading and Trailing

11029 - Leading and Trailing

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1970

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 result. However we can get at least some satisfaction if we could know few of the leading and trailing digits. This is the requirement of this problem.

 

Input

 

The first line of input will be an integer T<1001, where T represents the number of test cases. Each of the next T lines contains two positive integers, n and kn will fit in 32 bit integer and k will be less than 10000001.

 

Output

 

For each line of input there will be one line of output. It will be of the format LLL…TTT, where LLL represents the first three digits of n^k and TTT represents the last three digits of n^k. You are assured that n^k will contain at least 6 digits.

 

 

Sample Input

Output for Sample Input

2

123456 1

123456 2

123...456

152...936




大致的题意是:要求n的k次方,但是n和k非常大的时候没有办法去求(实际上可以模拟的,暂且不论),所以题目要求的数据只是n的k次方的前三位和后三位。
思路:刚看到题便一眼想到用同余摸定理求出后三位,直接求的话太慢,干脆二分幂吧!但是出题人的用意显然不只是在此,前三位的求法才是卡人的地方,想了半天,最终用double保存n的k次方,相当于缩小了一定倍数,比如,n的k次方等于344958……那么化成double就是0.344……然后取小数点后3位乘以1000即可!
code:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int mod=1000;
const int M=1000000000;
const int N=0x7fffffff;
ll mi (int n,int m)
{
if (m==1) return n%mod;
if (m%2==0)
return (mi(n,m/2)%1000)*(mi(n,m/2)%mod)%mod;
else
return n*mi(n,m/2)*mi(n,m/2)%mod;
}
double mi2(int n,int m)
{
if (m==0) return 1.0;
double s=mi2(n,m/2)*mi2(n,m/2);
if (m%2)
s*=n;
while (s>M) s/=M;
return s;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n,k;
cin>>n>>k;
int b=mi(n,k)%mod;
double c=mi2(n,k);
//cout<<c<<endl;
while (c>=1000) c/=10;
int d=c*1000;
while (d>=1000) d/=10;
printf("%d...%03d\n",d,b);
}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/382050.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

goland远程调试Docker

开发环境 goland windows10 Docker centos IP:123.57.43.91 操作原理及流程 goland通过tcp/8080(应该选取2375&#xff0c;登录阿里云开启这个端口&#xff0c;这里临时采用)端口与docker-host通信&#xff0c; 发送docker指令&#xff0c;然后让linux执行&#xff0c;通过d…

博客同步测试

该博客通过word发布&#xff01;

gorm踩的坑

gorm的那些坑 1. db.SingularTable(true) 在Gorm中&#xff0c;表名是结构体名的复数形式&#xff0c;列名是字段名的蛇形小写。即&#xff0c;如果有一个user表&#xff0c;那么如果你定义的结构体名为&#xff1a;User&#xff0c;gorm会默认表名为users而不是user。 db.S…

Go 语言实现 23 种设计模式(修饰器)

修饰器 修饰器模式就是在不改变对象内部结构的情况下&#xff0c;动态扩展它的功能。 Example_one type Object func(string) stringfunc Decorate(fn Object) Object {return func(base string) string {ret : fn(base)ret ret " and Tshirt"return ret} }func…

紫书的训练计划——一点点来,坚持到底!

先做 第10章3 数论的一点补充。然后趁热温习 10.1 和10.2 &#xff08;可能会有重的题目&#xff09; &#xff08;期望&#xff0c;概率的题目还要过段时间回来补坑&#xff09;然后 第7、8.1&#xff0c;8.2 章做暴力求解和高效算法&#xff08;可以适当的加快步伐&#…

Go 语言实现 23 种设计模式 单例模式

Go 语言实现 23 种设计模式 单例模式 单例模式 单例模式是一种常用的软件设计模式&#xff0c;在使用过程中&#xff0c;单例对象的类只有一个实例。使用单例模式&#xff0c;1 可以节省内存等资源&#xff0c;例如windows操作系统的资源管理器只有一个就够了。2 方便配置管理…

Go 语言实现 23 种设计模式适配器

Go 语言实现 23 种设计模式适配器 将一个类型的接口转换成客户希望的另外一个接口&#xff0c;使原本由于接口不兼容而不能一起工作的类可以一起工作。 Example_one package mainimport "fmt"// Adaptee 适配者 type MyLegacyPrinter struct{}func (l *MyLegacyPr…

go设计模式思维导图

go设计模式思维导图

uva 1610——Party Games

题目链接&#xff1a;http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id51171 题意&#xff1a;给你n个串的集合D&#xff0c;然后求一个长度最短的串s&#xff0c;使得使得s大于等于D中一半的串&#xff0c;又同时小于另一半串。 思路&#xff1a;直接暴力。先对…

链表相加 2. 两数相加

2. 两数相加 给出两个 非空 的链表用来表示两个非负的整数。其中&#xff0c;它们各自的位数是按照 逆序 的方式存储的&#xff0c;并且它们的每个节点只能存储 一位 数字。 如果&#xff0c;我们将这两个数相加起来&#xff0c;则会返回一个新的链表来表示它们的和。 您可以…

uva10780 - Again Prime? No time

uva10780 - Again Prime? No timeAgain Prime? No time. The problem statement is very easy. Given a number n you have to determine the largest power of m, not necessarily prime, that divides n!. Input The input file consists of several test cases. The first…

303. 区域和检索 - 数组不可变

303. 区域和检索 - 数组不可变 给定一个整数数组 nums&#xff0c;求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和&#xff0c;包含 i, j 两点。 示例&#xff1a; 给定 nums [-2, 0, 3, -5, 2, -1]&#xff0c;求和函数为 sumRange() sumRange(0, 2) -> 1 sumRange…

【转载】最短路径之Dijkstra算法详细讲解

&#xff11; 最短路径算法 在日常生活中&#xff0c;我们如果需要常常往返A地区和B地区之间&#xff0c;我们最希望知道的可能是从A地区到B地区间的众多路径中&#xff0c;那一条路径的路途最短。最短路径问题是图论研究中的一个经典算法问题&#xff0c; 旨在寻找图&#xf…

Intelij 添加php注释

没有methodParameters()) groovyScript("def result; def params\"${_1}\".replaceAll([\\\\[|\\\\]|\\\\s], ).split(,).toList(); for(i 0; i < params.size(); i) {result * param params[i] ((i < params.size() - 1) ? \\n:)}; return result…

HDU 2544 最短路(各种最短路算法的实现)

链接&#xff1a; http://acm.hdu.edu.cn/showproblem.php?pid2544 题目&#xff1a; Problem Description 在每年的校赛里&#xff0c;所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候&#xff0c;却是非常累的…

为什么LeetCode过一段时间又不会了

怎么从这个迷宫的左上角走到右下角&#xff1f; 看起来好像很简单&#xff0c;花时间还是可以找得到答案。 看了答案之后可以很清楚&#xff0c;这是正解。 只有一个问题&#xff1a; 我怎么就没立刻想到&#xff1f; 当在看题解或者听别人讲授思路的时候&#xff0c;你就是…

148. 排序链表

148. 排序链表 给你链表的头结点 head &#xff0c;请将其按 升序 排列并返回 排序后的链表 。 示例 1&#xff1a; 输入&#xff1a;head [4,2,1,3] 输出&#xff1a;[1,2,3,4] 示例 2&#xff1a; 输入&#xff1a;head [-1,5,3,4,0] 输出&#xff1a;[-1,0,3,4,5] 示…

POJ 3660 Cow Contest(传递闭包floyed算法)

Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest is…

23. 合并K个升序链表

23. 合并K个升序链表 给你一个链表数组&#xff0c;每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中&#xff0c;返回合并后的链表。 示例 1&#xff1a; 输入&#xff1a;lists [[1,4,5],[1,3,4],[2,6]] 输出&#xff1a;[1,1,2,3,4,4,5,6] 解释&#xf…

HDU 1874 畅通工程续 (Dijkstra , Floyd , SPFA, Bellman_Ford 四种算法)

畅通工程续 题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1874 Problem Description 某省自从实行了很多年的畅通工程计划后&#xff0c;终于修建了很多路。不过路多了也不好&#xff0c;每次要从一个城镇到另一个城镇时&#xff0c;都有许多种道路方案可以…