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. 【转发】【小程序】微信小程序日常开发中常遇到的错误代码

    还在为看不懂小程序错误状态码纠结吗?这里推荐一篇文章 重要的事情说三遍:原文链接  https://www.cnblogs.com/webonline/p/7528778.html 作者:玩世不恭. ...

  2. drupal7,注册成功之后想跳转到指定页面,该怎么破?

     1.hook sigup form alter,修改跳转地址 .还没试过 2.安装一下logintoboggan模块,里面有个注册后跳转到哪个页面的设置 这个对于不写代码的是比较方便的方法    3 ...

  3. JavaWEB SSH文件上传

    一.提交表单的<form> method属性必须为post  并且添加enctype="multipart/form-data" 属性 前台: <td>上传 ...

  4. 空间数据的WKT和WKB表现形式

    WKT(well-known text)是一种文本标记语言,该格式由开放地理空间联盟(OGC)制定,用于表示矢量数据中的几何对象,在数据传输与数据库存储时,常 用到它的二进制形式,即WKB(well- ...

  5. 分享泛微公司OA系统用于二次开发的sql脚本

    本单位用的oa系统就是泛微公司的oa协同办公平台,下面是我对他进行二次开发统计用到的写数据库脚本,只做开发参考使用,对于该系统的二次开发技术交流可以加我q:2050372586 [仪表盘]格式sql编 ...

  6. needPrint 不显示打印按钮

     客户问题:       客户用的是needPrint 来显示打印按钮,现在访问不能显示后台提示有错误 打开控制台显示: java.security.AccessControlException: ...

  7. linux 链接命令

    ln link /bin/ln -s 创建软链接ln -s [原文件] [链接文件] 软链接 ln -s /etc/issue /tmp/issue.soft硬链接ln /etc/issue /tmp ...

  8. mysql 日期时间类型

    datetime timestamp year date time drop table test;create table test (dt datetime, ts timestamp, y ye ...

  9. 封装NSMapTable并简易的使用

    封装NSMapTable并简易的使用 NSMapTable是弱引用的字典,可以用来存储对象,该对象消失了也没有关系,对于控制器越级跳转相当有用:) WeakDictionary.h 与 WeakDic ...

  10. 北美Developer生存发展攻略

    http://www.followmedoit.com/bbs/forum.php?mod=viewthread&tid=49&extra=page%3D1 在北美,做PM不易,而做D ...