(2017多校训练第二场)HDU - 6050 Funny Function 找规律 + 逆元

题目链接:点击打开链接

比赛的时候思路跑偏了,总是纠结于找第一行的每个元素对于答案的贡献,找了一个多小时然后放弃了。

暴力打表然后找规律。


因为存在除法取模的问题,所以要用到逆元。

代码如下:

#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;
const int MAX_N = 1000;
const int MOD = 1e9 + 7;

// 拓展欧几里得算法
void extgcd(LL a, LL b, LL& x, LL& y)
{
    if (!b)
    {
        x = 1;
        y = 0;
    }
    else
    {
        extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }
}

LL mod_pow(LL a, LL n, LL mod)
{
    LL res = 1;
    while (n)
    {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main()
{
    //freopen("test.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    cin.sync_with_stdio(false);
    int T;
    LL n, m;
    cin >> T;
    while (T--)
    {
        cin >> n >> m;
        if (m == 1)
        {
            cout << 1 << endl;
            continue;
        }
        LL x, y;
        extgcd(3, MOD, x, y);
        x = (x % MOD + MOD) % MOD; // x为3模1e9+7的逆元
        int ans;
        if (n % 2)
            ans = (mod_pow(mod_pow(2, n, MOD) - 1, m - 1, MOD) * 2 % MOD + 1) * x % MOD;
        else
            ans = (mod_pow(mod_pow(2, n, MOD) - 1, m - 1, MOD) * 2 % MOD) * x % MOD;
        cout << ans << endl;
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值