<hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字
1060题意很简单,求n的n次方的值的最高位数,我们首先设一个数为a,则可以建立一个等式为n^n = a * 10^x;其中x也是未知的;
两边取log10有:lg(n^n) = lg(a * 10^x);
即:n * lg(n) - x = lg(a);
现在就剩x一个变量了,我们知道x是值n^n的位数-1,a向下取整就是我们要求的数;
所以 按着上面的推导式翻译成代码就可以了(注意:数值的范围和之间的强制转换):
/*
* > File Name: 1060.cpp
* > Author: Ddlm2wxm
* > Mail: Ddlm2wxm@163.com
* > Created Time: Wed 23 Nov 2016 09:36:14 PM CST
***************************************************************/ #include<iostream>
#include<cmath>
#include <cstdio>
using namespace std; typedef long long ll;
typedef long double ld; int main() {
int n;
ll num, ans;
ld t;
scanf("%d", &n);
while (n--) {
scanf("%lld", &num);
t = num * log10(num);
ans = pow (, (t - (ll)t));
printf("%lld\n", ans);
}
return ;
}
1060
1061和1060题意一样,只不过这道题是要求最低位上即个位上的数.在之间循环的时候有一些技巧:
①如果是偶数的话,直接第一次循环n * n % 10,然后更换n的值为n * n % 10;这样下一次乘的时候就是两个n和两个n来相乘了,所以我们需要另外找一个变量来进行n的减半操作;循环到底即可。
②如果是奇数的话,在进行减半操作之后,就会变成(n - 1) / 2的数,自然就会少一次n,所以我们需要当n为奇数时候使用res记录一下n的值。循环到底即可。
闲话不多说,上代码:
/*
* > File Name: 1061.cpp
* > Author: Ddlm2wxm
* > Mail: Ddlm2wxm@163.com
* > Created Time: Wed 23 Nov 2016 09:40:03 PM CST
*****************************************************************/ #include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std; int mod_exp (int n) {
int res = , t = n % , b = n;
while (b) {
if (b & ) {
res *= t;
res %= ;
}
t *= t;
t %= ;
b >>= ;
}
return res;
} int main() {
int T, n;
scanf ("%d", &T);
while (T--) {
scanf ("%d", &n);
cout << mod_exp (n) << endl;
}
return ;
}
1061
欢迎码友一起评论更简单的方法,一起成长。
<hdu - 1600 - 1601> Leftmost Digit && Rightmost Digit 数学方法求取大位数单位数字的更多相关文章
- Rightmost Digit(快速幂+数学知识OR位运算) 分类: 数学 2015-07-03 14:56 4人阅读 评论(0) 收藏
C - Rightmost Digit Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- HDU Rightmost Digit
Rightmost Digit Time Limit:1000MS Memory Limit: ...
- 题解报告:hdu 1061 Rightmost Digit(快速幂取模)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- 快速幂 HDU 1061 Rightmost Digit *
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDOJ 1061 Rightmost Digit
找出数学规律 原题: Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- hdoj 1061 Rightmost Digit【快速幂求模】
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDOJ 1061 Rightmost Digit(循环问题)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
随机推荐
- Web前端开发的一点记录
工欲善其事必先利其器,开发工具选择Sublime Text 简称(ST) 本文所说的均在Windows NT 环境下使用的ST3运行. ST的Package Control安装方法: 1. 直接输入p ...
- SZU : A11 Sequence
Description We are given a integer sequence, your job is find the length of the longest contiguous s ...
- Moq & RhinoMocks
Moq & RhinoMocks 使用Mock对象进行测试一般都会有以下三个关键步骤: 使用接口来描述需要测试的对象 为实际的产品代码实现这个接口 以测试为目的,在Mock对象中实现这个接口 ...
- iOS制作Static Library(静态库),实现多工程的连编
在iOS开发中,我们会发现一些偏底层或基础代码是直接可以复用的,当我们换一个项目,改变的只需要是偏上层的业务逻辑代码,所以我们可以把这部分基础代码制作为一个静态库static library,并不断扩 ...
- UIKit类结构图
- Jenkins中关于一些插件的使用
Jenkins中关于一些插件的使用方法 最近在为公司搭建CI平台过程中,以及在具体项目实施过程中使用过的一些插件的具体用法: 1. ant插件 这个插件可能是我们最为经常使用的,若构建脚本是使用bui ...
- C#中易混淆的知识点
C#中易混淆的知识点 一.引言 今天在论坛中看到一位朋友提出这样的一个问题,问题大致(问题的链接为:http://social.msdn.microsoft.com/Forums/zh-CN/52e6 ...
- Organic Solar Cells - Generations of Solar Cells
Sunlight --> Electricity A. E. Becquerel, 1839 . He stated that we can get energy from sunlight. ...
- Javascript内存泄漏
Javascript内存泄漏 原文:http://point.davidglasser.net/2013/06/27/surprising-javascript-memory-leak.html 本周 ...
- mysql数据类型简介
MySQL的数据表类型很多,其中比较重要的是MyISAM,InnoDB这两种. 这两种类型各有优缺点,需要根据实际情况选择适合的,MySQL支持对不同的表设置不同的类型.下面做个对比: MyISAM表 ...