数论(一)LOJ1282
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的更多相关文章
- Codeforces Round #382 Div. 2【数论】
C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...
- NOIP2014 uoj20解方程 数论(同余)
又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...
- 数论学习笔记之解线性方程 a*x + b*y = gcd(a,b)
~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不 ...
- hdu 1299 Diophantus of Alexandria (数论)
Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)
4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 290 Solved: 148[Submit][Status ...
- bzoj2219: 数论之神
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- hdu5072 Coprime (2014鞍山区域赛C题)(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...
- ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德
POJ 1061 青蛙的约会 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu Descr ...
- 数论初步(费马小定理) - Happy 2004
Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...
随机推荐
- django中自定义表名及字段名称
在meta 类中指定表名,在字段中通过db_column指定列名如下所示 class Record(models.Model): content=models.CharField(max_length ...
- Java Socket, DatagramSocket, ServerSocketChannel io代码跟踪
Java Socket, DatagramSocket, ServerSocketChannel这三个分别对应了,TCP, udp, NIO通信API封装.JDK封装了,想跟下代码,看下具体最后是怎么 ...
- final关键字介绍
许多程序设计语言都有自己的办法告诉编译器某个数据是“常数”.常数主要应用于下述两个方面: (1) 编译期常数,它永远不会改变 (2) 在运行期初始化的一个值,我们不希望它发生变化 对于编译期的常数,编 ...
- Linux 网络(连接)相关参数作用
参考: [1] http://bbs.chinaunix.net/thread-2318039-1-1.html Backlog net.core.netdev_max_backlog = 1000 ...
- .NET开源工作流RoadFlow-表单设计-下拉列表
下拉列表即<select></select>标签,相关选项与单选按钮组相同: 是否添加空项:即在下拉列表的开头是否添加一个空的选项<option value=" ...
- mvc4 坑啊
昨天下午出了个BUG.到今天上午才解决掉.就是mvc页面的属性名跟controller 中action 参数的名相同.导致action无法取得前台的值.这个问题浪费了很多时间.命名要规范. 如 页面 ...
- SVN学习——简单入门之创建仓库、导入、检出(一)
从刚刚参加工作就开始使用svn,清晰的记得那年师姐比较生气的来找我:“你怎么又不更新就提交代码了,把我写的都给覆盖掉了”,哈哈~ 虽然一直在用svn,不过在日常工作中主要用到的是简单的操作,而且大多数 ...
- June 15th 2017 Week 24th Thursday
Whatever is worth doing is worth doing well. 任何值得做的,就把它做好. Whatever is worth doing is worth doing we ...
- 设计模式——工厂模式(Factory Method)
工厂方法模式,定义一个用于创建对象的接口,让子类决定实例化哪个类.工厂方法使一个类的实例化延迟到其子类. UML图: 运算基类: package com.cnblog.clarck; /** * 数据 ...
- PowerDesigner如何设计表之间的关联
PowerDesigner如何设计表之间的关联 步骤/方法 在工具箱中找到参照关系工具: 由地区表到省份表之间拉参照关系,箭头指向父表,然后双击参照关系线,打开参照关系的属性: 在这里检查 ...