Description

The function
f(n, k) is defined by f(n, k) = 1k + 2k + 3k +...+
nk. If you know the value of n and k, could you tell us the last digit of
f(n, k)?

    For example, if n is 3 and k is 2, f(n,
k
) = f(3, 2) = 12 + 22 + 32 = 14. So the last digit of
f(n, k) is 4.

Input

The first line has an integer
T (1 <= T <= 100), means there are T test cases.

    For each test case, there is only one line with two integers n,
k
(1 <= n, k <= 109), which have the same meaning as above.

Output

For each test case, print the last digit of
f(n, k) in one line.

Sample Input


10
1 1
8 4
2 5
3 2
5 2
8 3
2 4
7 999999997
999999998 2
1000000000 1000000000

Sample Output


1
2
3
4
5
6
7
8
9
0

打表发现n的循环周期可以是100,所以直接n%100,发现m的循环周期可以是4,所以只要在a[100]储存4个数

#include<stdio.h>
#include<string.h>
int a[100];
int quick(int a,int b)
{
int ans=1;
a=a%10;
while(b>0)
{
if(b%2==1)
ans=ans*a%10;
b=b/2;
a=(a*a)%10;
}
return ans;
} int main()
{
int i,n,k,t,j,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
n=n%100;
for(i=1;i<=4;i++)
{
t=0;
for(j=1;j<=n;j++)
{
t=(t+quick(j,k))%10;
}
a[i]=t;
}
a[0]=a[4];
k=k%4;
printf("%d\n",a[k]);
}
return 0;
}

另一种是先找循环节,再算


#include<stdio.h>
#include<string.h>
int powermod(int n,int k){
int ans=1;
n=n%10;
while(k){
if(k%2) ans=(ans*n)%10;
k=k/2;
n=(n*n)%10;
}
return ans;
}
int main(){
int T,i,j,n,k;
scanf("%d",&T);
while(T--){
int f[1111]={0};
int ans=0,t=0;
scanf("%d%d",&n,&k);
for(i=1;i<=1000;i++){
t=powermod(i,k);
f[i]=(t+f[i-1])%10;
}
int temp,flag;
for(i=1;i<=1000;i++){
flag=1;
for(j=i+1;j<=1000;j++){
if(f[j]!=f[j%i]) {flag=0; break;}
}
if(flag) {temp=i; break;}
}
ans=n%temp;
printf("%d\n",f[ans]);
}
}

最新文章

  1. python2.7高级编程 笔记二(Python中的描述符)
  2. Trigger和ViewStateManager的具体比较
  3. WDA导出文件XLS,WORD
  4. oracle date change
  5. innodb的锁时间
  6. 获取本机IP、mac地址、计算机名
  7. 容器 SET part2
  8. 【转】window.scroll 浏览器滚动条的参数总结
  9. stl 和并查集应用
  10. java笔试要点(java多线程)
  11. 【JavaScript】数组
  12. 使用TLS证书保护Docker
  13. golang初识2
  14. ImageView获取宽高
  15. Git和Github入门教程
  16. Linux 忘记了mysql 密码
  17. 最短路(Bellman)-Hdu1874畅通工程序
  18. IDEA中配置JUnit单元测试
  19. JAVA 利用Dom4j实现英语六级词汇查询 含演示地址
  20. LOJ#6118 鬼牌

热门文章

  1. 常用 .gitignore 模板
  2. kubernets之Deployment资源
  3. K8s遇到问题解决思路
  4. 01-CentOS 8.1安装 Docker
  5. CentOS 7 下安装 mysql ,以及用到的命令
  6. 10_1_OS模块
  7. 处理 K8S Orphaned pod found - but volume paths are still present on disk 孤儿pod
  8. 最佳的思维导图生成工具——markmap 使用教程
  9. apk开发环境!多亏这份《秋招+金九银十-腾讯面试题合集》跳槽薪资翻倍!再不刷题就晚了!
  10. grpc-metadata