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]);
}
}

C - Last Digit的更多相关文章

  1. [LeetCode] Nth Digit 第N位

    Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n i ...

  2. [LeetCode] Number of Digit One 数字1的个数

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  3. [Leetcode] Number of Digit Ones

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  4. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  5. kaggle实战记录 =>Digit Recognizer

    date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...

  6. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  7. Last non-zero Digit in N!(阶乘最后非0位)

    Last non-zero Digit in N! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  8. POJ3187Backward Digit Sums[杨辉三角]

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6350   Accepted: 36 ...

  9. Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  10. Java for LeetCode 233 Number of Digit One

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

随机推荐

  1. Go语言从入门到放弃(设置 go get 为国内源)

    前言 Go语言学到 Gin 框架了, 其实每天学习是比较辛苦的事情, 坚持下去! 在使用 Go 过程中发现, 最无奈的是Go的一些模块下不下来, 即便挂了V, 油管2k不卡的那种, 依旧是 time ...

  2. Flutter 布局类组件:线性布局(Row和Column)

    前言 所谓线性布局,即指沿水平或垂直方向排布子组件.Flutter中通过Row和Column来实现线性布局,并且它们都继承自弹性布局(Flex). 接口描述 Row({ Key key, // 表示子 ...

  3. python optparse模块的用法

    引用原博主文章链接: https://www.cnblogs.com/darkpig/p/5717902.html

  4. C# datagridview设置标题为汉语

    正常情况下,在给datagridview绑定数据源之后,显示的是SQL语句中的栏位,如下 我们想让标题显示汉语,可以有一下两种方法 1.在SQL中设置列别名 SELECT TITLE AS '报警标题 ...

  5. postgresql中权限介绍

    postgresql权限分为实例的权限,数据库的权限,模式的权限,对象的权限,表空间的权限 实例的权限:由pg_hba.conf文件控制,控制那些用户那些IP以哪种方式连接数据库 数据库的权限:是否允 ...

  6. druid discard long time none received connection问题解析

    最新项目中用的druid连接数据库遇到一个困扰很久的问题 1 开始用的druid版本是1.1.22版本,由于业务需求,单个连接需要执行很久,理论上不需要用到自动回收,但为了安全,还是加了自动回收,时间 ...

  7. 如何封装Promise对象?

    最近看到了一个有趣的Promise的方法,这里记录下来 <script> class MyPromise { constructor(executor) { // 初始化state赋值为p ...

  8. 解决 browser-sync start --server --files 文件不能同步的问题!

    解决 browser-sync start --server --files 文件不能同步的问题! 请看我的源命令: browser-sync start --server --file 'test2 ...

  9. https://www.cnblogs.com/wclwcw/p/7515515.html

    https://www.cnblogs.com/wclwcw/p/7515515.html

  10. 前置时间(Lead Time),也称前置期、备货周期

    https://wiki.mbalib.com/wiki/前导时间 什么是前导时间 所谓的前导时间(leading time),就是产品从设计,到生产.物流.销售的过程. BELLWETHER:&qu ...