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. js-用于检测类数组对象的函数

    //判定o是否是一个类数组对象 //字符串和函数有length属性,但是它们 //可以用typeof检测将其排除.在客户端JavaScript中,DOM文本节点 //也有length属性,需要用额外判 ...

  2. centos 删除文件和目录

    每次都记不住,发个文章记录一下.直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 删除文件夹 ...

  3. MySQL创建数据库/表等基本命令操作

    前提:安装好MySQL并且配置好服务,能够正常使用 按住键盘上的Windows图标,通过搜索框找到MySQL5.7 Command Line Client,点击启动 输入安装时设置用户的密码 成功连接 ...

  4. iframe的探讨

    用法 1.iframe是用来在网页中插入第三方页面,早期的页面使用iframe主要是用于导航栏这种很多页面都相同的部分,这样在切换页面的时候避免重复下载. 优点 1.便于修改,模拟分离,像一些信息管理 ...

  5. flutter实现(OutlineButton)线框按钮

    在flutter的控件里 常用按钮有:FlatButton,RaisedButton,FloatingActionButton,OutlineButton. FlatButton是扁平的,没有阴影的. ...

  6. fuzz实战之honggfuzz

    Honggfuzz实战 前言 本文介绍 libfuzzer 和 afl 联合增强版 honggfuzz .同时介绍利用 honggfuzz 来 fuzz 网络应用服务. 介绍 honggfuzz 也是 ...

  7. C# Base64Helper

    public static class Base64Helper { /// <summary> /// base64字符保存图片到本 /// </summary> /// & ...

  8. 三、vue如何配置路由 、获取路由的参数、部分刷新页面、缓存页面

        1.路由配置:所有的启动文件都在最初始的main.js文件里面,这个文件中首先需要引入: 2.路由文件配置说明: 3.如何获取页面url的参数? this.$route.query 4.页面之 ...

  9. 【Java】操作mysql数据库

    package bd; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; im ...

  10. The Go scheduler

    转载自:http://morsmachine.dk/go-scheduler Introduction One of the big features for Go 1.1 is the new sc ...