Teemo's formula - 公式推导

Teemo has a formula and he want to calculate it quickly.

The formula is .

As the result may be very large, please output the result mod 1000000007.

Input Format

The input contains several test cases, and the first line is a positive integer T indicating the number of test cases which is up to 10^5.

For each test case, the first line contains an integer n(1<=n<=10^9).

Output Format

For each test case, output a line containing an integer that indicates the answer.

样例输入

2
2
3

样例输出

6
24

 

解题思路:

a(n) = n*(n+1)*2^(n-2)

http://oeis.org/

 

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007   //必须宏定义,传参会超时

typedef long long ll;  

ll ModMul(ll a,ll b)//快速积取模 a*b%n
{
    ll ans=0;
    while(b)
    {
        if(b&1)
            ans=(ans+a)%mod;
        a=(a+a)%mod;
        b>>=1;
    }
    return ans;
}

ll ModExp(ll a,ll b)//快速幂取模 a^b%n
{
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=ModMul(ans,a);
        a=ModMul(a,a);
        b>>=1;
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll n;
        scanf("%lld",&n);
        if(n==1)
        printf("%lld\n",1);
        else
        printf("%lld\n",ModMul(ModMul(n,n+1),ModExp(2,n-2)));
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值