C - Last Digit
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的更多相关文章
- [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 ...
- [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 ...
- [Leetcode] Number of Digit Ones
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 【Codeforces715C&716E】Digit Tree 数学 + 点分治
C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...
- kaggle实战记录 =>Digit Recognizer
date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...
- [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_ ...
- 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 ...
- POJ3187Backward Digit Sums[杨辉三角]
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6350 Accepted: 36 ...
- Number of Digit One
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- 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 ...
随机推荐
- Haproxy-1.8.20 根据路径(URI)转发到后端不同集群
HAProxy根据不同的URI 转发到后端的服务器组 1 ) 实验内容说明: 1.1 ) 根据不同的URI 转发到后端的服务器组. /a /b 和其他 默认使用其他. 1.2 ) 使用IP介绍: ha ...
- linux服务开机自启动&注册系统服务
首先先看下linux系统开机启动顺序,如下图 对,要解决Linux CentOS 和 Red Hat Linux 系统中设置服务自启动有两种方式,就是从图中圈出的两个步骤下手. 一.修改 /etc/r ...
- 如何跑通第一个 SQL 作业
简介: 本文由阿里巴巴技术专家周凯波(宝牛)分享,主要介绍如何跑通第一个SQL. 一.SQL的基本概念 1.SQL 分类 SQL分为四类,分别是数据查询语言(DQL).数据操纵语言(DML).数据定义 ...
- 【Linux】cp命令的各种妙用
CP 功能: 复制文件或目录 说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文件 ...
- 【Linux】一个网卡部署多个内网ip
1.用root权限的用户登录CENTOS,进入network-scripts文件夹下(本步骤可以省略,与二步骤一起完成): shell命令:cd /ect/sysconfig/network-scri ...
- CTS相关的几个表
TMSALOG/TMSALOGAR :STMS传输日志表 TMSCNFS:传输组 TMSCDOM:传输域 TMSCDES:传输目的地 TMSBUFTXT:传输请求的短文本和用户 TMSCSYS:tms ...
- MySQL索引性能分析
为什么要做性能分析 你有没有这样的情况. 面对一个你没怎么写过的.复杂的业务,你构思了很久,终于开始敲下了第一段代码. 写的过程迷迷糊糊,有的时候还能把自己搞晕了. 但你还是终于把它写完了. 但是点击 ...
- AWS IoT Greengrass是什么?V1和V2版本及其差异
AWS IoT Greengrass Greengrass主要是用于边缘计算或者机器学习有关,对于详细了解请阅读结尾处的官方文档,文档内容也较为丰富. 目录 AWS IoT Greengrass ...
- SW3516中文资料书
SW3516 是一款高集成度的快充车充芯片, 支持 A+C 口任意口快充输出, 支持双口独立限流.其集成了 5A 高效率同步降压变换器, 支持 PPS/PD/QC/AFC/FCP/SCP/PE/SFC ...
- linux串口编程
按照对linux系统的理解,串口编程的顺序无非就是open,read,write,close,而串口有波特率.数据位等重要参数需要设置,因此还应该用到设置函数,那么接下来就带着这几个问题去学习linu ...