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. OpenLayers 3 实现划线,画点

    //划线 this.drawLine = function (type) { //清除双击放大效果 this.map.getInteractions().item(1).setActive(false ...

  2. XMLHttpRequest.responseType

    "arraybuffer" "blob" "document" "json" "text"

  3. spark编程python实例

    spark编程python实例 ValueError: Cannot run multiple SparkContexts at once; existing SparkContext(app=PyS ...

  4. 04_Spring中使用Quartz

    [Spring中使用SimplerTrigger] [QuartzTask.java] package com.higgin.task; import java.text.SimpleDateForm ...

  5. Appcan开发之页面布局与CSS排版(转)

    在Appcan开发中,首先要进行的就是页面布局,然后在这个页面上填充数据,加上互动元素,最终构成完整的应用. 因为appcan是使用HTML5+CSS3+JavaScript技术来进行开发,所以与普通 ...

  6. JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录

    下面以购物App常用的收藏功能为例,讲述如何在 JMeter 中使用 cookie 进行登录态操作.具体的操作步骤如下所示: 第一步.录制查看收藏列表的 HTTP请求: 录制查看收藏列表的HTTP请求 ...

  7. js 时间特效

    http://example.com:1234/test.htm#part2:Hash的作用. http://www.cnblogs.com/Interkey/p/RunAsAdmin.html

  8. 设计模式之简单工厂模式(Simple Factory)

    原文地址:http://www.cnblogs.com/BeyondAnyTime/archive/2012/07/06/2579100.html 今天呢,要学习的设计模式是“简单工厂模式”,这是一个 ...

  9. pb getchild获取DropDownDW子窗体后进行取值

    datawindowchild ldwc if dw_1.getchild("dwmc", ldwc) = 1 then  long ll_f  ll_f = ldwc.find( ...

  10. bootstrap-multiselect.js多选下拉框初始化时默认选中初始值

    bootstrap-multiselect.js多选下拉框默认值设置 一.案例数据格式  二.HTML代码 <select id="msgRoles" multiple=&q ...