NEFU1493 fast power plus division (inverse element)

Title:

Gugu has two infinite length sequences A,B
A0=a^0/0!,A1=a^1/1!,A2=a^2/2!,A3=a^3/3!....
B0=0, B1=b^1/1!,B2=0,B3=b^3/3!,B4=0, B5=b^5/5! ...
Douge found these two sequences very troublesome, so he came up with a good idea. He wanted to combine these two sequences with a sequence C
Cn= n! * sigma Ai*B(n-i) (n-i)>=0 i>=0
When Douge wrote the C sequence on the paper, it felt so much better!!! So he just wants to know Cn, but Douge is going to play games, so he wants you to help him
Input
Group t data (T < = 1E5)
Give a, B, N 0 < = a, B, n < = 1e9
Output
Cn%1e9+7
**Sample Input
1
1 1 1
Sample Output
1
Hint
About 1e5 sets of data, scanf is recommended
Example explanation:
C1=1!((A0*B1)+(A1*B0))=1!((1/0! 1/1!) + (1/1! * 0)) = 1

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
long long mod=1e9+7;
long long qm(long long a,long long b,long long m)
{  long long ans=1;
    while(b)
    { if(b%2==1)
       {
           ans=(ans*a)%m;
       }
       a=(a*a)%m;
       b=b>>1;
    }
    return ans;
}
int main()
{  int t;
   long long a,b,n,ans;
   scanf("%d",&t);
   while(t--)
   {   scanf("%lld %lld %lld",&a,&b,&n);
       if(n==0&&a!=0&&b!=0) printf("1\n");
       else
       {
      long long m1,m2,tmp;
       m1=(a+b);m2=a-b;
       tmp=qm(2,mod-2,mod);
       ans=(((qm(m1,n,mod)-qm(m2,n,mod)+mod)%mod)*qm(2,mod-2,mod))%mod;//**this+mod In particular, many mistakes**
       /*WhenThere are two formulas written by time-division parity, and the answer is right, but there is also a special pit
       if(n%2==0)
     {  long long m1,m2,tmp;
       m1=b+a;m2=b-a;
       tmp=(qm(m1,n,mod)-qm(m2,n,mod)+mod)%mod;//plus mod
        ans=(tmp*qm(2,mod-2,mod))%mod;
         printf("%lld\n",ans);
   }
     else
     {  long long m1,m2,tmp;
       m1=b+a;m2=b-a;
        tmp=(qm(m1,n,mod)+qm(m2,n,mod)+mod)%mod;//plus mod,Because ( b-a)It could be a negative number
        ans=(tmp*qm(2,mod-2,mod))%mod;
        printf("%lld\n",ans);
     }

*/
 printf("%lld\n",ans);
       }
   }

    return 0;
}

**

Posted by eraxian on Wed, 01 Jan 2020 03:44:05 -0800