Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Description
Solution
- will into , and i can be into 2i, then according to the form of this equation is clearly is the same
- But we are due to enumerate , so we only need an even number of items. But we can see that if the number is odd, it must also leave 。
- You only need to take the last integer like, leaving a The contribution of odd items.
- Fast power can be.
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll __int128
using namespace std;
int T;
ll A,B,n,p;
void read(ll &x){
x=0; char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar());
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
}
int pd[50];
void write(ll x){
if (!x) {printf("0\n");return;}
while (x) pd[++pd[0]]=x%10,x/=10;
while (pd[0]) putchar(pd[pd[0]--]+'0');
puts("");
}
struct num{
ll x,y;
num(ll _x,ll _y){x=_x,y=_y;}
};
num operator *(num a,num b){return num((a.x*b.y+b.x*a.y)%p,(a.x*b.x%p*A+a.y*b.y)%p);}
ll qp(){
num s=num(0,1),x=num(1,B);
for(;n;n/=2,x=x*x) if (n&1)
s=s*x;
return s.y;
}
int main(){
freopen("ceshi.in","r",stdin);
scanf("%d",&T);
while (T--){
read(n),read(A),read(B),read(p);
A=A%p,B=B%p;
write(qp());
}
}