NBUT-[1674] math happy
2017-05-15 20:51
78 查看
NBUT-[1674] math happy 时间限制: 1000 ms 内存限制: 65535 K
问题描述
计算1^1+2^2+3^3…….+n^n的和的个位数
输入
输入整数 T (T个case) 输入整型 n (n < 10^18)
输出
输出算式的答案的个位数
样例输入
3 1 2 3
样例输出
1 5 2
ac 代码:
/* 题意: 计算1^1+2^2+3^3.......+n^n的和的个位数 注意 n 的范围 为1e18 :需要用LL 输入。 思路: 首先想到的是快速幂。快速幂在求单个幂时特别快。但本题数据大,暴力将n个数的幂模加起来肯定tlx。 想到打表 发现1^1, .... , n^n 在每20个有个循环。 table[] 记录。 然后想直接用(long long)sum 记录每一次的值,但是还超了; 于是想到 每个数的和是否也有循环节。于是发现每200个就要循环一次。 ans[x] 记录每个x 的 table[1%20]+...+table[x%20]; 输出直接打表就好。 */ #include<iostream> using namespace std; typedef long long LL; int table[21]; int ans[201]; LL fast_mi(LL a,LL b,int mod) { LL base = a,re=1; while(b) { if(b&1) re=(re*base)%mod; base=(base*base)%mod; b>>=1; } return re; } int main() { //打表。 for(int i=1;i<=20;i++) table[i]=fast_mi(i,i,10); table[0]=table[20]; int sum=0; for(int i=1;i<=200;i++) { sum+=table[i%20]; ans[i]=sum%10; } ans[0]=ans[200]; //main int t; cin>>t; while(t--) { LL num; cin>>num; cout<<ans[num%200]<<endl;; } return 0; }
相关文章推荐
- nbut 1674
- Class bytes found but defineClass()failed for: 错误解决
- Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock
- Everything you always wanted to know about the processing of customer exit variables, but …
- 转:使用Maven创建多module工程时,子module的pom文件报 must be "pom" but is "jar" 错误。
- Angularjs refresh but jump to the home page
- django-redis遇到MISCONF Redis is configured to save RDB snapshots, but is currently not able to persis
- 问题:expected single matching bean but found 2
- NBUT 1221 Intermediary 2010辽宁省赛
- Eclipse is running in a JRE, but a JDK is required
- PE 题目 NBUT 1226 A Simple Triangle
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- 【Visual Studio】The project appears to be under source control, but the associated source control plug-in is not installed on this computer
- 打包出错 "XXX" is translated here but not found in default locale
- popupWindow,Attempted to finish an input event but the input event receiver has already been dispose
- is expected to be of type 'org.com.ssh.hibernate.AOPService.EchoTest' but was actually of type 'com.
- com.alibaba.fastjson.JSOnException:exepct '[' ,but {object
- PID file found but no matching process was found. Stop aborted
- stylus-loader@2.5.1 requires a peer of stylus@>=0.52.4 but none was installed 错误