【CSU1437】Last Digit(打表找规律)

题目链接

1347: Last Digit

Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 306     Solved: 191    


Description

    The function f(nk) is defined by f(nk) = 1k + 2k + 3k +...+ nk. If you know the value of n and k, could you tell us the last digit of f(nk)?
    For example, if n is 3 and k is 2, f(nk) = f(3, 2) = 12 + 22 + 32 = 14. So the last digit of f(nk) 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 nk (1 <= nk <= 109), which have the same meaning as above.

Output

    For each test case, print the last digit of f(nk) 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

Hint

Source

中南大学第一届长沙地区程序设计邀请赛

解题思路:

打表可以发现最后一个数字是有个循环周期的,然后直接暴力找周期就可以了。

代码:

#include<bits/stdc++.h>
typedef long long LL;
const int maxn=1e4+5;
using namespace std;
LL a[maxn];
LL quickpow(LL a,LL b){
    LL ans=1;
    while(b!=0){
        if(b&1)ans=ans*a%10;
        a=a*a%10;
        b>>=1;
    }
    return ans%10;
}
int main(){
    int T;
    scanf("%d",&T);
    /*for(int i=1;i<=1005;i++)
    {
        LL x=quickpow(i,2);
        a[i]=(x+a[i-1])%10;
        printf("%lld\n",a[i]);
    }*/
    while(T--)
    {
        LL n,k,ans;
        scanf("%lld%lld",&n,&k);
        memset(a,0,sizeof(a));
        for(int i=1;i<=maxn-1;i++)
        {
            LL x=quickpow(i,k);
            a[i]=(x+a[i-1])%10;
        }
		int x,zq;
		for(int i=1;i<=maxn-1;i++){
			int flag=1;
			for(int j=i+1;j<=maxn-1;j++){
				if(a[j]!=a[j%i])
                {
                    flag=0;
                    break;
                }
			}
			if(flag)
            {
                zq=i;
                break;
            }
		}
		printf("%lld\n",a[n%zq]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/81743278
今日推荐