1347: Last Digit (周期函数)
1347: Last Digit
Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 309 Solved: 191
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
Hint
Source
要你求f(n,k)函数值的最后一位
因为结果也是要你求最后一位,最后一位只能通过最后一位影响
比如i的k次方的最后一位和i+1的k次方的最后一位相加 影响的也是结果的最后一位
可以先打一个表
输出前面1000位看一下
看看能不能找到规律
然后发现果然有规律.....
其实这种题肯定是有周期性的
做多了就知道
然后就是找出周期
因为不找出周期的话,数据太太了,这提直接暴力是写不出来的
找到周期之后
n模周期就好了
这样n的大小变小了
但是n对应的值还是没有变的
周期函数嘛
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<memory.h>
#include<memory>
using namespace std;
#define max_v 1005
#define max_n 1000
typedef long long LL;
int f[max_v];
int qm(int n,int k)
{
int ans=;
n=n%;
while(k)
{
if(k%) ans=(ans*n)%;
k=k/;
n=(n*n)%;
}
return ans;
}
int main()
{
int t;
int n,k;
LL temp;
scanf("%d",&t);
while(t--)
{
memset(f,,sizeof(f));
scanf("%d %d",&n,&k);
for(int i=; i<max_n; i++)
{
temp=qm(i,k);
f[i]=(temp+f[i-])%;
}
int flag;
int index;
for(int i=; i<max_n; i++)
{
flag=;
for(int j=i+; j<=max_n; j++)
{
if(f[j]!=f[j%i])
{
flag=;
break;
}
}
if(flag)
{
index=i;
break;
}
}
int ans=n%index;
printf("%d\n",f[ans]);
}
return ;
}
/*
题目意思:
要你求f(n,k)函数值的最后一位 先通过快速幂得到i的k次方的最后一位
因为结果也是要你求最后一位,最后一位只能通过最后一位影响
比如i的k次方的最后一位和i+1的k次方的最后一位相加 影响的也是结果的最后一位
可以先打一个表
输出前面1000位看一下
看看能不能找到规律
然后发现果然有规律.....
其实这种题肯定是有周期性的
做多了就知道
然后就是找出周期
因为不找出周期的话,数据太太了,这提直接暴力是写不出来的
找到周期之后
n模周期就好了
这样n的大小变小了
但是n对应的值还是没有变的
周期函数嘛
*/
1347: 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 ...
- Activity系列讲解---三大基本状态与七大生命周期函数
简介:四大组件之一,在应用中一个Activity可以用来表示一个界面,可以理解为用户可视化界面,一个android应用必须通过Activity来运行和启动. 1.三大基本状态与七大生命周期函数 2.代 ...
- [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 ...
随机推荐
- MySql基本学习知识点:
1.Mysql的简介: (1):常识: MySQL是一种关系数据库管理系统,是一种开源软件 由瑞典MySQL AB公司开发,2008年1月16号被Sun公司收购.2009年,SUN又被Oracle收购 ...
- JavsScript--on与addEventListener的使用与两者的不同
Js之on和addEventListener的使用与不同 一.首先介绍两者的用法: 1.on的用法:以onclick为例 第一种: obj.onclick = function(){ //do som ...
- 使用servicestack连接redis
引言:作为少有的.net架构下的大型网站,stackoverflow曾发表了一篇文章,介绍了其技术体系,原文链接http://highscalability.com/blog/2011/3/3/sta ...
- clipChildren属性
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- @Secured()、 @PreAuthorize() 、 @RolesAllowed()
在Spring security的使用中,为了对方法进行权限控制,通常采用的三个注解,就是@Secured().@PreAuthorize().@RolesAllowed(). 示例,修改用户密码必须 ...
- .hivehistory
在当前用户的家目录下有个.hivestory文件,里面存放了用户执行的hive操作记录,如下: [hadoop@hadoop1 hive-0.14]$ cat ~/.hivehistory show ...
- c# 通过html导出pdf,带分页
通过NuGet安装 PechkinPechkin.Synchronized 一下示例是控制台应用程序 static void btnCreate() { SynchronizedPechkin sc ...
- [转载]Cool, Tomcat is able to handle more than 13,000 concurrent connections
Last time I have promised you to take a look at more real life scenario regarding threads. In the la ...
- 连接AWS Ubuntu服务器
1.在AWS上创建了Ubuntu实例后,在实例里点连接.点使用PuTTY连接,下载PuTTY软件. 2.在所有程序里找到PuTTYgen并打开,点Load选择创建实例时的pem文件,点save pri ...
- 转: C# 的结构剖析
原文链接:http://www.cnblogs.com/jiajiayuan/archive/2011/09/20/2181582.html 本文意在巩固基础知识,并不是对其进行深入剖析,还望理解.本 ...