2267 - MiaoWu's Function

Given n,k and the function f(n,k) = 1^k + 2^k + ... + n^k , you're supposed to calculate it.

题目输入

Multiple test cases.

Each line contains two numbers n,k(1<=n,k<=10^9)

题目输出

Output the last digit of the function f(n,k)

输入/输出样例

题目输入

1 1
8 4
2 5
3 2
5 2
8 3
2 4

题目输出

1
2
3
4
5
6
7

C++解答

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int fun(int a,int b){
    int ans=1;
    while(b){
        if(b&1) b--,ans=(ans*a)%10;
        else    b/=2,a=(a*a)%10;
    }
    return ans;
}
int solve(int n,int k){
    k%=4;
    k+=4;
    if(n>10) return (((n/10)*solve(10,k))%10+solve(n%10,k))%10;
    int ans=0;
    for(int i=1;i<=n;i++)
        ans=(ans+fun(i,k))%10;
    return ans;
}
int main(){
    int n,k;
    while(scanf("%d%d",&n,&k)!=EOF){
        printf("%d\n",solve(n,k));
    }
    return 0;
}


时间限制 1 秒
内存限制 128 MB
讨论 统计
上一题 下一题