hdu 6050 Funny Function(快速幂)

来源:互联网 发布:北京超图软件 编辑:程序博客网 时间:2024/05/22 00:31

Funny Function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1210    Accepted Submission(s): 592


Problem Description
Function Fx,ysatisfies:

For given integers N and M,calculate Fm,1 modulo 1e9+7.
 

Input
There is one integer T in the first line.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
 

Output
For each given N and M,print the answer in a single line.
 

Sample Input
22 23 3
 

Sample Output
233



暴力打表枚举比较小的n, m的最后结果,找到规律



ans=2(2n1)m13,2(2n1)m1+13,n & 1 == 0n & 1 == 1


赛场上看到这道题,能勇敢的去这么做的真的需要勇气

由于公式还涉及到除法,所以可能需要更多一点的时间

#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<map>#include <bits/stdc++.h>using namespace std;const int N = 1100000+10;typedef long long LL;const LL mod = 1e9+7;char str[N];stack<LL>st;LL cal(LL x,LL n){    LL r=1;    while(n)    {        if(n&1) r=r*x%mod;        n>>=1;        x=x*x%mod;    }    return r;}int main(){    int t;    scanf("%d", &t);    while(t--)    {        LL n, m;        scanf("%lld %lld", &n, &m);        if((n&1)==0)        {            printf("%lld\n",(2*cal((cal(2,n)-1),m-1))*cal(3,mod-2)%mod);        }        else        {            printf("%lld\n",(2*cal((cal(2,n)-1),m-1)+1)*cal(3,mod-2)%mod);        }    }    return 0;}








ans=2(2n1)m13,2(2n1)m1+13,n & 1 == 0n & 1 == 1
原创粉丝点击