C - Last Digit
来源:互联网 发布:淘宝固定背景尺寸 编辑:程序博客网 时间:2024/06/05 18:24
Description
The functionf(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 off(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 off(n, k) is 4.
Input
The first line has an integerT (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 off(n, k) in one line.
Sample Input
101 18 42 53 25 28 32 47 999999997999999998 21000000000 1000000000
Sample Output
1234567890打表发现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]);}}
0 0
- C - Last Digit
- C - Last Digit
- ZOJ 3108 Last Digit
- UVA 10162 - Last Digit
- The last digit
- UVa:10162 Last Digit
- UVa 10162 - Last Digit
- UVA 10162 Last Digit
- UVA - 10162 Last Digit
- COJ 1347 Last Digit
- uva 10162 - Last Digit(数论)
- The Last Non-zero Digit
- Last Digit By Factorial Divide
- poj 3406:Last digit(求C(n, m)的最后一个非0位)
- PKU 1150 The Last Non-zero Digit
- hdu1066 Last non-zero Digit in N!
- hdu1066 Last non-zero Digit in N!
- poj 1150 The Last Non-zero Digit
- p51 第二章 第四题 潘璠
- UVA 10347 - Medians(计算几何)
- 《Cocos2d-x实战 工具卷》上线了
- nginx+php之任意类型(.py)解释为php配置方式
- VK Cup 2015 - Qualification Round 1 A. Reposts(map最大连续长度)
- C - Last Digit
- 目前见到的最Make Sense的Python Guide
- C语言union关键字
- HDU 1557 权利指数
- VS2005的数据断点功能
- 【BZOJ2427】【HAOI2010】软件安装 tarjan+树形背包DP
- ios中发短信功能实现的几种方式
- Express 高级架构 OpenCart 主题模板 ABC-0024
- FIFO与GPIF的区别