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

来源:互联网 发布:网络招标注册流程 编辑:程序博客网 时间:2024/05/16 23:47



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;
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,(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
21 22 3
 

Sample Output
313
 

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

#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 longint 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;
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,(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
21 22 3
 

Sample Output
313
 

0 0
原创粉丝点击