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

A Boring Question

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5793

Description


764119-20160804173305559-605626236.png

Input


The first line of the input contains the only integer T,
Then T lines follow,the i-th line contains two integers n,m.

Output


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

Sample Input


2
1 2
2 3

Sample Output


3
13

Source


2016 Multi-University Training Contest 6


题意:


用m个不大于n的数构成一个序列,对每个序列求C(ki+1,ki)的连乘积.
对所有可能的序列,累加上述连乘积.


题解:


还是打表找的规律...(好弱啊)
f(1,2)=3; f(2,2)=7;
f(1,3)=4; f(2,3)=13;
f(1,4)=5; f(2,4)=21;
f(1,5)=6; f(2,5)=31;
......
打了个5*5的表后发现规律:(后附打表代码)
f(n,m) = f(n-1,m) + m^n;
= m^0 + m^1 + m^2 + ... + m^n; (等比数列求和)
= (1 - m^(n+1)) / (1 - m);
然后用快速幂和乘法逆元求出上式即可.

官方题解:
764119-20160804173333887-1251465462.png


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 2100
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL x,y,gcd;
void ex_gcd(LL a,LL b)
{
    if(!b) {x=1;y=0;gcd=a;}
    else {ex_gcd(b,a%b);LL temp=x;x=y;y=temp-a/b*y;}
}

LL quickmod(LL a,LL b,LL m) {
    LL ans = 1;
    while(b){
        if(b&1){
            ans = (ans*a)%m;
            b--;
        }
        b/=2;
        a = a*a%m;
    }
    return ans;
}

int main(int argc, char const *argv[])
{
    //IN;

    int t; cin >> t;
    LL n, m;
    while(scanf("%I64d %I64d", &n,&m) != EOF)
    {
        LL ans1 = quickmod(m, n+1, 1000000007LL) - 1;
        LL ans2 = m - 1;

        ex_gcd(ans2, 1000000007LL);
        while(x < 0) {
            x+=1000000007LL;
            y-=ans2;
        }

        LL ans = (ans1 * x) % mod;

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

    return 0;
}
打表代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 2100
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL e[510][510];
void make(){
    for(int i=0;i<510;i++)
        e[i][0]=1;
    for(int i=1;i<510;i++)
        for(int j=1;j<510;j++)
            e[i][j]=(e[i-1][j-1]+e[i-1][j])%mod;
}

int n,m,ans;
void fun(int len, vector<int> cur, int last) {
    if(len == m) {
        int tmp = 1;
        for(int i=1; i<cur.size(); i++) {
            tmp *= e[cur[i]][cur[i-1]];
        }
        ans += tmp;
        return;
    }

    for(int i=last; i<=n; i++) {
        cur.push_back(i);
        fun(len+1, cur, i);
        cur.pop_back();
    }
}

int main(int argc, char const *argv[])
{
    //IN;

    make();

    for(n=0; n<=5; n++) {
        for(m=2; m<=5; m++) {
            ans = 0;
            vector<int> cur; cur.clear();
            fun(0,cur,0);
            printf("%d-%d : %d\n", n,m,ans);
        }
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5737627.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值