[Comet OJ - Contest # 13]. D leather fire mouse - no anxiety of heart -

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.
This link: https://blog.csdn.net/qq_43649416/article/details/102761613

Description

Portal
Here Insert Picture Description
T < = 1 e 5 group number according to each group read Enter n , a , b , p ( 1 n , a , b , p 1 0 18 ) T <= 1e5 sets of data, each read n, a, b, p (1≤n, a, b, p≤10 ^ {18})

Solution

  • will a i a^i into ( a ) 2 i (\sqrt{a})^{2i} , and i can be into 2i, then according to the form of this equation is clearly ( a + b ) n (a+b)^n is the same
  • But we are due to enumerate 2 i 2i , so we only need an even number of items. But we can see that if the number is odd, it must also leave a \sqrt{a}
  • You only need to take ( a + b ) n (\sqrt{a}+b)^n the last integer like, leaving a k a k \sqrt{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());
	}
}

Guess you like

Origin blog.csdn.net/qq_43649416/article/details/102761613
Recommended