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. Modernizing Business Process with Cloud and AI

    The world is awash with digital transformation. Every customer and partner that I talk to, across ev ...

  2. ios虚拟机安装(四)

    Virtual Machine Settings: Processors(Virtual Intel VT-X/EPT or AMD-V/RVI 一定要打勾) Floppy(软驱) (必须remove ...

  3. 数组转换list集合问题

    1问题的起因: 项目总要使用一个方法把数组转换成list集合,百度说有这个方法Arrays.asList可以实现 2结果报错 抛出java.lang.UnsupportedOperationExcep ...

  4. UBUNTU16.04 连接不了cn.archive.ubuntu.com

    ubuntu系统更换源 更换源的方法非常简单:修改/etc/apt/sources.list文件即可 进入目录 /etc/apt cd /etc/apt修改sources.list文件 sudo vi ...

  5. leetcode 之 Same Tree

    1.题目描述 Given two binary trees, write a function to check if they are the same or not. Two binary tre ...

  6. linux上设置mysql编码

    linux下设置mysql编码 linux下设置mysql编码 首先查找MySql的cnf文件的位置: [root@flyHome gaoxiang]# find / -iname '*.cnf' - ...

  7. 新一代 javascript 模板引擎:artTemplate-3.0

    特性 性能卓越,执行速度通常是 Mustache 与 tmpl 的 20 多倍(性能测试) 支持运行时调试,可精确定位异常模板所在语句(演示) 对 NodeJS Express 友好支持 安全,默认对 ...

  8. Entity Framework工具POCO Code First Generator的使用

    在使用Entity Framework过程中,有时需要借助工具生成Code First的代码,而Entity Framework Reverse POCO Code First Generator是一 ...

  9. ORA-06512 问题解决

    SQL> select * from dba_role_privs where grantee='SUK';     GRANTEE GRANTED_ROLE ADMIN_OPTION DEFA ...

  10. Linux xargs命令详解

    find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部 xargs要处理的文件如果不是在结尾,需要加上 -i这个参数 xargs常见命令参数 args:xarg ...