C - Last Digit
2024-08-23 10:18:53
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]);
}
}
最新文章
- python2.7高级编程 笔记二(Python中的描述符)
- Trigger和ViewStateManager的具体比较
- WDA导出文件XLS,WORD
- oracle date change
- innodb的锁时间
- 获取本机IP、mac地址、计算机名
- 容器 SET part2
- 【转】window.scroll 浏览器滚动条的参数总结
- stl 和并查集应用
- java笔试要点(java多线程)
- 【JavaScript】数组
- 使用TLS证书保护Docker
- golang初识2
- ImageView获取宽高
- Git和Github入门教程
- Linux 忘记了mysql 密码
- 最短路(Bellman)-Hdu1874畅通工程序
- IDEA中配置JUnit单元测试
- JAVA 利用Dom4j实现英语六级词汇查询 含演示地址
- LOJ#6118 鬼牌
热门文章
- 常用 .gitignore 模板
- kubernets之Deployment资源
- K8s遇到问题解决思路
- 01-CentOS 8.1安装 Docker
- CentOS 7 下安装 mysql ,以及用到的命令
- 10_1_OS模块
- 处理 K8S Orphaned pod found - but volume paths are still present on disk 孤儿pod
- 最佳的思维导图生成工具——markmap 使用教程
- apk开发环境!多亏这份《秋招+金九银十-腾讯面试题合集》跳槽薪资翻倍!再不刷题就晚了!
- grpc-metadata