HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)

A Boring Question

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 865    Accepted Submission(s): 534

Problem Description

There are an equation.
∑0≤k1,k2,?km≤n∏1?j<m(kj+1kj)%1000000007=?
We define that (kj+1kj)=kj+1!kj!(kj+1?kj)! . And (kj+1kj)=0 while kj+1<kj.
You have to get the answer for each n and m that given to you.
For example,if n=1,m=3,
When k1=0,k2=0,k3=0,(k2k1)(k3k2)=1;
Whenk1=0,k2=1,k3=0,(k2k1)(k3k2)=0;
Whenk1=1,k2=0,k3=0,(k2k1)(k3k2)=0;
Whenk1=1,k2=1,k3=0,(k2k1)(k3k2)=0;
Whenk1=0,k2=0,k3=1,(k2k1)(k3k2)=1;
Whenk1=0,k2=1,k3=1,(k2k1)(k3k2)=1;
Whenk1=1,k2=0,k3=1,(k2k1)(k3k2)=0;
Whenk1=1,k2=1,k3=1,(k2k1)(k3k2)=1.
So the answer is 4.

Input

The first line of the input contains the only integer T,(1≤T≤10000)
Then T lines follow,the i-th line contains two integers n,m,(0≤n≤109,2≤m≤109)

Output

For each n and m,output the answer in a single line.

Sample Input

2
1 2
2 3

Sample Output

3
13

Author

UESTC

Source

2016 Multi-University Training Contest 6

Recommend

wange2014

题解:

找规律...
f(0,2)=1; f(1,2)=3; f(2,2)=7; f(3,2)=15
f(0,3)=1; f(1,3)=4; f(2,3)=13; 
f(0,4)=1; f(1,4)=5; f(2,4)=21;
f(0,5)=1; f(1,5)=6; f(2,5)=31;

f(n,m)=f(n-1,m)*m+1

所以 f(n,m)=(m^(n+1)-1)/(m-1)

//#include <iostream>
#include<bits/stdc++.h>

using namespace std;

const long long mod=1000000007;
long long ans,n,m;
int T;
long long poww(long long a,long long b)
{
    long long ans=1;
    while(b)
    {
        if (b%2==1) ans=(ans*a)%mod;
        a=(a*a)%mod;
        b/=2;
    }
    return ans;
}

int main()
{
   scanf("%d",&T);
   for(;T>0;T--)
   {
       scanf("%lld%lld",&n,&m);
       long long ans=poww(m,n+1);
       ans=(ans+mod-1)%mod;
       ans=(ans*poww(m-1,mod-2))%mod;
       printf("%lld\n",ans);
   }
   return 0;
}
时间: 2024-10-18 20:14:13

HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)的相关文章

HDU 5793 A Boring Question (费马小定理) ---2016杭电多校联合第六场

A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 156    Accepted Submission(s): 72 Problem Description There are an equation.∑0≤k1,k2,?km≤n∏1?j<m(kj+1kj)%1000000007=?We define t

ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies (打表找规律+快速幂)

题目链接:https://nanti.jisuanke.com/t/31716 题目大意:有n个孩子和n个糖果,现在让n个孩子排成一列,一个一个发糖果,每个孩子随机挑选x个糖果给他,x>=1,直到无糖果剩余为止.给出数字n,问有多少种分发糖果的方法. 样例输入 复制 1 4 样例输出 复制 8 解题思路:我们可以这样想,一个糖果的话,应该是只有1种方法记为x1,如果是两个糖果的话,有两种方法即为x2,分别为(1,1)和(2),从中我们可以想到如果n个糖果的话,就可以分为第n个人取1个的话就有x(

[2018 ACM-ICPC 焦作赛区网络赛] G - Give Candies(找规律+快速幂)

题目链接 There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1...N)(1...N), and each time a child is invite

hdu-4990 Reading comprehension(快速幂+乘法逆元)

题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:1024000000,1024000000"

hdu 4686 Arc of Dream(矩阵快速幂乘法)

Problem Description An Arc of Dream is a curve defined by following function: where a0 = A0 ai = ai-1*AX+AY b0 = B0 bi = bi-1*BX+BY What is the value of AoD(N) modulo 1,000,000,007? Input There are multiple test cases. Process to the End of File. Eac

Happy 2004(快速幂+乘法逆元)

Happy 2004 问题描述 : Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your job is to determine S modulo 29 (the rest of the division of S by 29). Take X = 1 for an example. The positive integer divisors of 2

hdu-5690 All X(快速幂+乘法逆元)

题目链接: All X Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description F(x,m) 代表一个全是由数字x组成的m位数字.请计算,以下式子是否成立: F(x,m) mod k ≡ c Input 第一行一个整数T,表示T组数据.每组测试数据占一行,包含四个数字x,m,k,c 1≤x≤9 1≤m≤10^10 0≤c<k≤10,000 Ou

HDU 4572 Bottles Arrangement(找规律,仔细读题)

题目 //找规律,123321123321123321…发现这样排列恰好可以错开 // 其中注意题中数据范围: M是行,N是列,3 <= N < 2×M //则猜测:m,m,m-1,m-1,m-2,m-2,……,2,2,1,1求出前m个数字的和就是答案. //发现案例符合(之前的代码第二天发现案例都跑不对,真不知道我当时眼睛怎么了) #include <iostream> #include<stdio.h> #include<string.h> #inclu

HDU 2147-kiki&#39;s game(博弈/找规律)

kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others) Total Submission(s): 9174    Accepted Submission(s): 5485 Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his