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的k次方的最后一位和i+1的k次方的最后一位相加 影响的也是结果的最后一位
可以先打一个表
输出前面1000位看一下
看看能不能找到规律
然后发现果然有规律.....
其实这种题肯定是有周期性的
做多了就知道
然后就是找出周期
因为不找出周期的话,数据太太了,这提直接暴力是写不出来的
找到周期之后
n模周期就好了
这样n的大小变小了
但是n对应的值还是没有变的
周期函数嘛
code:
#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 (周期函数)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. Activity系列讲解---三大基本状态与七大生命周期函数

    简介:四大组件之一,在应用中一个Activity可以用来表示一个界面,可以理解为用户可视化界面,一个android应用必须通过Activity来运行和启动. 1.三大基本状态与七大生命周期函数 2.代 ...

  4. [Leetcode] Number of Digit Ones

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...

  5. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  6. kaggle实战记录 =>Digit Recognizer

    date:2016-09-13 今天开始注册了kaggle,从digit recognizer开始学习, 由于是第一个案例对于整个流程目前我还不够了解,首先了解大神是怎么运行怎么构思,然后模仿.这样的 ...

  7. [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_ ...

  8. 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 ...

  9. POJ3187Backward Digit Sums[杨辉三角]

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6350   Accepted: 36 ...

随机推荐

  1. Debian Gun/linux基本用法

    添加软件源:vim /etc/apt/sources.list 在文本中添加如下内容:deb http://mirrors.163.com/debian/ stretch main non-free ...

  2. CSS属性之attr()

    attr()准确的说,不应该是一个属性,而是一个CSS的函数,我们先看看MDN上的介绍吧: Summary The attr() CSS function is used to retrieve th ...

  3. Java基础学习—思维导图

    找到两张Java学习的思维导图,特别适合我这样的菜鸟学习,贴过来和小伙伴分享.

  4. Hive安装与应用过程

    1.  参考说明 参考文档: https://cwiki.apache.org/confluence/display/Hive/GettingStarted 2.  安装环境说明 2.1.  环境说明 ...

  5. settimeout、setinterval区别和相互模拟

    前几天翻书,看到“避免双重求值”一节时有提到settimeout().setinterval() 建议传入函数而不是字符串以作为第一个参数,所以这里总结一下settimeout()和setinterv ...

  6. phantomjs在centos7下的安装

    http://phantomjs.org/download.html 下载 Linux 64-bit 解压 tar jxvf 安装包名称.tar.bz2 pwd 查看路径 ln -sf phantom ...

  7. 单机安装hive和presto

    问题: 公司最近在搞presto,主要是分析一下presto和hive的查询大数据量的性能对比: 我先把我的对比图拿出来(50条数据左右)针对同一条sql(select * from employee ...

  8. Ehcache.xml 配置及属性说明

    1.配置样例 <?xml version="1.0" encoding="UTF-8"?> <ehcache> <diskStor ...

  9. [C++] 用Xcode来写C++程序[1] 新建C++项目工程

    用Xcode来写C++程序[1] 新建C++项目工程 第一节从新建工程并编译C++源码开始 新建工程 源码: // // main.cpp // YeHelloWorld // // Created ...

  10. Mysql源码安装shell脚本

    #!/bin/bash#date:2019/1/20#by author zhangjia#install mysql#shell_name:mysql_auto_install.sh######## ...