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.
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.
For each test case, print the last digit of f(n, k) in one line.
10 1 1 8 4 2 5 3 2 5 2 8 3 2 4 7 999999997 999999998 2 1000000000 1000000000Sample Output
1 2 3 4 5 6 7 8 9 0
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll mod_pow(ll x,ll n,ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
ll a[10];
//printf("==%lld\n",mod_pow(0,5,10));
while(T--){
ll n,k;
scanf("%lld%lld",&n,&k);
//memset(a,0,sizeof(a));
ll sum=0;
for(int i=0;i<10;i++){
a[i]=mod_pow(i,k,10);
//printf("++%lld\n",a[i]);
sum=(sum+a[i])%10;
}
//printf("---%lld\n",sum);
ll res=0;
res=n/10*sum%10;
//printf("res=%lld\n",res);
for(int i=0;i<=n%10;i++){
res+=a[i];
}
printf("%lld\n",res%10);
}
return 0;
}