Alex and Number

Alex and Number

时间限制: 1 Sec  内存限制: 128 MB
提交: 69  解决: 12
[提交][状态]

题目描述

Alex love Number theory. Today he think a easy problem, Give you number N and L, To calculate N^1 + N^2 + N^3 + ... + N^L, And ans will mod 1e9+7

输入

Many test of input(about 10W case), each input contains number N and L, 1 <= N, L <= 2147483647

输出

Contains a number with your answer

样例输入

2 5
3 5
10 10

样例输出

62
363
111111033

discuss:1e9+7,数好大有木有,2147483647的2147483647是多少~point:快速幂
#include<iostream>
#include<cstring>
#include<cstring>
#include<cstdio>

using namespace std;

const int MOD = 1000000007;  // 宏定义也行

typedef long long ll;  // __int64也行

ll pow_mod(ll x, ll k)   // 快速幂函数
{
    ll ans = 1;

    if(k == 1)
        return x;
    while(k)
    {
        if(k&1)
            ans = (ans * x) % MOD;
        x = (x * x) % MOD;
        k >>= 1;     // k  /= 2
    }
    return ans; //     (n*n*n*n*n)%Mod = (( ((n*n)%Mod) *((n*n)%Mod) ) % Mod) * (n % Mod)  ,记住就行了~至于为什么,目前我还不知道
}

ll magic(ll n, ll l)
{
    if(l == 1)
        return n; 

    ll tmp = magic(n, l/2);  // 递归,二分,先求l部分的前一半项

    ll ste = (tmp + (pow_mod(n, l/2) * tmp )% MOD) % MOD;  // n+n*n+n*n*n+n*n*n*n = n+n*n + (n+n*n) * n * n(L个

    if(l&1)
        ste = (ste + (pow_mod(n, l) % MOD)) % MOD;  // 如果l是奇数,加上最后一项,即n的l次方
    return ste;  // 返回当前l项的结果
}

int main()
{
    ll n, l;

    while(~scanf("%lld%lld", &n, &l))
    {
        printf("%lld\n", magic(n, l));
    }
    return 0;
}
时间: 2025-01-04 09:50:34

Alex and Number的相关文章

python攻克之路day1

day1内容 1 .............................数据类型介绍.............................. 计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值, 还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义不同的数据类型.在Python中,能够直接处理的数据类型有以下几种 python数据类型基本结构: 整数 ---------->  num = 1 布尔型

hdu3709---Balanced Number(数位dp)

Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit. When a pivot is placed at some digit of the nu

From MSI to WiX, Part 2 - ARP support, by Alex Shevchuk

Following content is directly reprinted from From MSI to WiX, Part 2 - ARP support Author: Alex Shevchuk Adding Add/Remove Program (ARP) support Subset of properties stored in the Property table defines the information operating system will show in A

sdut2623——The number of steps

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

NBUT校赛 J Alex’s Foolish Function(分块+延迟标记)

Problem J: Alex’s Foolish Function Time Limit: 8 Sec  Memory Limit: 128 MB Submit: 18  Solved: 2 Description Alex has an array F which size is n, initially each element’s value is zero. Now he wants to operate the array, he has 4 different functions:

From MSI to WiX, Part 1 - Required properties, by Alex Shevchuk

Following content is directly reprinted from From MSI to WiX, Part 1 - Required properties Author: Alex Shevchuk Introduction Today I will start a series of posts about creating an MSI installation package using WiX.  The goal here is to show what is

Alex 的 Hadoop 菜鸟教程: 第10课 Hive 入门教程

Hive 安装 相比起很多教程先介绍概念,我喜欢先动手装上,然后用例子来介绍概念.我们先来安装一下Hive 先确认是否已经安装了对应的yum源,如果没有照这个教程里面写的安装cdh的yum源http://blog.csdn.net/nsrainbow/article/details/36629339 Hive是什么 Hive 提供了一个让大家可以使用sql去查询数据的途径.但是最好不要拿Hive进行实时的查询.因为Hive的实现原理是把sql语句转化为多个Map Reduce任务所以Hive非常

Alex 的 Hadoop 菜鸟教程: 第8课 Sqoop1 安装/导入/导出教程

靠!sqoop2的文档太少了,而且居然不支持Hbase,十分简陋,所以我愤而放弃Sqoop2转为使用Sqoop1,之前跟着我教程看到朋友不要拿砖砸我,我是也是不知情的群众 卸载sqoop2 这步可选,如果你们是照着我之前的教程你们已经装了sqoop2就得先卸载掉,没装的可以跳过这步 $ sudo su - $ service sqoop2-server stop $ yum -y remove sqoop2-server $ yum -y remove sqoop2-client 安装Sqoop

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b