Leftmost Digit
来源:互联网 发布:淘宝美工怎么接私活 编辑:程序博客网 时间:2024/05/26 22:59
HDU1060
题意:
给你一个数字N,求N^N的最左边数字(1<=N<=1,000,000,000)
思路:
这题先打表找了下规律,发现并没有HDU1061那样的规律.好吧,只能老老实实想了.
求最左边的数字,我们可以通过n^n/(n^n的位数-1)取得
一个数的位数就是int(log10(n))+1;
而n^n的位数就是int(n*log10(n))+1;
设m=n^n,两边取对数,log10(m)=n*log10(n)
左边移过去就是 m=10^(n*log10(n));
则 n^n=10^(n*log10(n));
所以n^n/(n^n的位数-1)=(10^(n*log10(n)))/(int(n*log10(n)))//(1已经约掉了)
=(10^(n*log10(n)))/(10^(int(log10(n*log10(n))))
=10^( n*log10(n)-int(n*log10(n) )
数据比较大,int要改为long long
Code:
#include<iostream>#include<cstdio>#include<cmath>using namespace std;typedef long long ll;int main(){ int t,n; double digit; double ans; scanf("%d",&t); while(t--) { scanf("%d",&n); // m=n^n log10(m)=n*log10(n) //n^n=m=10^(n*log10(n)) digit=n*log10(n); //cout<<"digit="<<digit<<endl; double tmp=digit-ll(digit); // cout<<"tmp="<<tmp<<endl; ans= pow(10.0,tmp);//n^n/(digit-1)就是最左边的数; cout<<int(ans)<<endl; // cout<<"ans="<<int(ans)<<endl;// ans=pow(10.0,n*log10(n)-temp+1); // n^n/(temp-1)jiu; // cout<<int(tmp)<<endl; }}
0 0
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Leftmost Digit
- Android获取内置和外置sd卡路径的方法
- LintCode:堆化
- 复平面
- LDA的python实现之模型参数训练
- Binary Tree Preorder Traversal
- Leftmost Digit
- 各种浏览器内核的区别
- HDU 1421 搬寝室
- 欢迎使用CSDN-markdown编辑器
- Linux C笔记3——Shell编程之选择语句
- gd32的rtc使用心得
- Android 中系统组件GridView属性笔记
- HDU4707 pet
- 理解JavaScript -- this call aply