hdu 5793A Boring Question (打表 + 乘法逆元 + 快速模)



Problem Description
There are an equation.
0k1,k2,kmn1j<m(kj+1kj)%1000000007=?
We define that  (kj+1kj)=kj+1!kj!(kj+1kj)!  . 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 ;
When k1=0,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=0,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=0,k2=0,k3=1,(k2k1)(k3k2)=1 ;
When k1=0,k2=1,k3=1,(k2k1)(k3k2)=1 ;
When k1=1,k2=0,k3=1,(k2k1)(k3k2)=0 ;
When k1=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 , (1T10000)
Then  T  lines follow,the i-th line contains two integers  n , m , (0n109,2m109)
 

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

Sample Input
  
  
2 1 2 2 3
 

Sample Output
  
  
3 13
 

思路:应该打表,找规律(等比数列求和),套下公式 用下乘法逆元。

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 10010
#define MOD 1000000007
#define LL long long

int T;
LL n,m;
const LL mod =1000000007;

//int a[15];
//int ff[10];
//int ans;
//
//int solve(int num)
//{
//    int tot=0;
//    if(num==0) return 0;
//    while(num)
//    {
//        a[tot++] = num%10;
//        num /=10;
//    }
//    if(a[tot-1]<a[0]) return 0;
//    int ans=ff[a[tot-1] - a[0]];
//    int re=ff[a[0]];
//
//    for(int i=0;i<tot;i++)
//    {
//        if(a[i]<a[i-1]) return 0;
//        else
//        {
//            int c=a[i] - a[i-1];
//            re *=ff[c];
//        }
//    }
//    return ans / re;
//}
//
//void dfs(int n,int len,int re)
//{
//    printf("%d\n",re);
//    if(len==0)
//    {
//        int cc=solve(re);
//        ans +=cc;
//        return ;
//    }
//    for(int i=0;i<=n;i++)
//        dfs(n,len-1,re*10 + i);
//}
//
//void init()
//{
//    ff[0]=0;
//    for(int i=1;i<=10;i++)
//        ff[i] = ff[i-1] * i;
//
//    for(int n=0; n<=9; n++)
//        for(int m=2; m<=5; m++)
//        {
//            ans=0;
//            dfs(n,m,0);
//           printf("dp[%d][%d]=%d\n", n,m,ans);
//        }
//}



LL inv(LL a,LL m)
{
    if(a==1) return 1;
    return inv(m%a,m) * (m-m/a) %m;
}

LL pow_m(LL a,LL b,LL m)
{
    LL ans=1;
    while(b)
    {
        if(b&1)
        {
            ans = ans * a%m;
        }
        a = a*a%m;
        b>>=1;
    }
    return ans;
}

int main()
{
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d%I64d",&n,&m);

        LL ans = (pow_m(m,n+1,mod) - 1 + mod) %mod * inv(m-1,mod) % mod;

        printf("%I64d\n",ans);
    }
    return 0;
}






Problem Description
There are an equation.
0k1,k2,kmn1j<m(kj+1kj)%1000000007=?
We define that  (kj+1kj)=kj+1!kj!(kj+1kj)!  . 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 ;
When k1=0,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=0,k3=0,(k2k1)(k3k2)=0 ;
When k1=1,k2=1,k3=0,(k2k1)(k3k2)=0 ;
When k1=0,k2=0,k3=1,(k2k1)(k3k2)=1 ;
When k1=0,k2=1,k3=1,(k2k1)(k3k2)=1 ;
When k1=1,k2=0,k3=1,(k2k1)(k3k2)=0 ;
When k1=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 , (1T10000)
Then  T  lines follow,the i-th line contains two integers  n , m , (0n109,2m109)
 

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

Sample Input
   
   
2 1 2 2 3
 

Sample Output
   
   
3 13
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值