1.题目来源LOJ1282

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
Output for Sample Input
5

123456 1

123456 2

2 31

2 32

29 8751919

Case 1: 123 456

Case 2: 152 936

Case 3: 214 648

Case 4: 429 296

Case 5: 665 669

2.题目分析

给定两个整数n和k,求出n^k的前三位和后三位

3.我的思路

虽然好像数据很大哦,估计要爆炸,但是要不要用java试一下呢
TLE代码如下:

import java.math.BigDecimal;
import java.util.Scanner;
import java.util.HashMap;
import java.math.BigInteger;
public class Main{
public static void main(String[] args) {
Scanner ind = new Scanner(System.in);
int T=ind.nextInt(),d=1;
while(T>1){
--T;
BigInteger a=ind.nextBigInteger();
int k=ind.nextInt();
BigInteger res=BigInteger.valueOf(1);
while(k>=1){
if(k%2==1)
{
res=res.multiply(a);
k--;
}
k=k/2;
a=a.multiply(a);
}
String s=res.toString();
System.out.print("Case ");
System.out.print(d);
d++;
System.out.print(": ");
System.out.print(s.substring(0, 3));
System.out.print(" ");
System.out.println(s.substring(s.length()-3, s.length()));
}
}
}

咳咳……结果还是爆了,还是没办法想的简单Orz.

好吧,其实换一种思路,不用把结构都求出来,只求前三位和后三位。
先看后三位,这个可以使用快速幂来做,模取1000即可。
关键是前三位:
推导过程如下:


所以最后,x的前m位数就为10m−1∗10b10m−1∗10b
图片来自http://blog.csdn.net/Dylan_Frank/article/details/52749665?locationNum=16
最后的最后,由于把整数和小数分开了,结果就可能出现00*或者0**,所以需要去掉前导零,就用%3d,要不还是WA…..Orz

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
int Pow_mod(int a, int b, int mod) {
int res = 1, temp;
a = a%mod, temp = a;
for (; b; b /= 2) {
if (b & 1) {
res = res * temp % mod; // 2进制上这一位为1,乘上这一位权值
}
temp = temp * temp % mod; // 位数加1, 权值平方
}
return res;
}
int main()
{
int n, k, T, count = 1;
cin >> T;
while (T-- > 0)
{
cin >> n >> k;
double a = k*log10(n);
a = a - (int)a;
double res1 = pow(10, a)*pow(10, 2);
int res2 = Pow_mod(n, k, 1000);
printf("Case %d: %d %03d\n",count,(int)res1,res2);
count++;
}
}

数论(一)LOJ1282的更多相关文章

  1. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  2. NOIP2014 uoj20解方程 数论(同余)

    又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...

  3. 数论学习笔记之解线性方程 a*x + b*y = gcd(a,b)

    ~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不 ...

  4. hdu 1299 Diophantus of Alexandria (数论)

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  5. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  6. bzoj2219: 数论之神

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  7. hdu5072 Coprime (2014鞍山区域赛C题)(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...

  8. ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德

    POJ 1061 青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu  Descr ...

  9. 数论初步(费马小定理) - Happy 2004

    Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...

随机推荐

  1. centos7 版本防火墻操作和配置

    1.关闭firewall:systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止fire ...

  2. Java使用POI操作Excel文件

    1.简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式文件读和写的功能. 2.依赖的jar包 <!-- ex ...

  3. ppt写作的注意事项

    PPT推荐字体及大小: 宋体严谨,适合正文,显示最清晰 黑体庄重,适合标题,或者强调区 隶书楷体,艺术性强,不适合投影 如果通过文字排版突出重点:加粗.加大字号.变色 PPT文字太多怎么办? 1.抽象 ...

  4. IS Decisions如何帮助企业提高安全标准

    PCI DSS标准有什么要求? 简单地说,PCI DSS要求最高级别的网络安全性.这一标准如今广泛应用于需要存储.管理.传输客户(或持卡人)个人数据的行业和领域. 施行严格的访问监控措施 为了保证关键 ...

  5. matlab练习程序(单源最短路径Bellman-Ford)

    该算法可以用来解决一般(边的权值为负)的单源最短路径问题,而dijkstra只能解决权值非负的情况. 此算法使用松弛技术,对每一个顶点,逐步减少源到该顶点的路径的估计值,直到达到最短的路径. 算法运算 ...

  6. javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/el/ELResolver错误解决办法

    今天不用eclipse.myeclipse等开发工具,纯手写JSP页面(有点作死)时突然出现以前从来没遇到过的问题,报错如下: HTTP Status 500 - java.lang.NoClassD ...

  7. 【Leetcode】【Easy】Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  8. Selenium2学习(十五)-- 单选框和复选框(radiobox、checkbox)

    本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...

  9. 怎样在linux下编写C程序并编译执行

    一.Hello, world! 在linux下输入:(以hello.c为例)首先选中文件要保存的路径(如:cd work)vi hello.c(要编辑的文件名) 输入程序:# include<s ...

  10. JQuery DataTable的配置项及事件

    当我们需要在加载或者显示表格的时候,改变jquery datatable 的数据,或增加一些html标签处理. 可以通过Jquery DataTable的回调函数处理. 实例代码: if (oTabl ...