传送门

神仙……这题有毒……

一直在那里考虑没有逆元怎么办然后考虑解exgcd巴拉巴拉

最后只好看题解了

而且这题龟速乘都不行……得用代码里那种叫人半懂不懂的方式取模……

//minamoto
#include<bits/stdc++.h>
#define R register
#define int long long
#define ld long double
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
int a,b,n,P,A,B,res;
int mul(R int x,R int y){
R int z=(ld)x*y/P;
R int res=x*y-z*P;
if(res<0)res+=P;
return res;
}
//int mul(int _a,int _b){
// int _c=(ld)(_a)*_b/P;
// int _ans=_a*_b-_c*P;
// if (_ans<0) _ans+=P;
// return _ans;
//}
int solve(int n){
if(n<=1)return n?(a+b)%P:1;
int res=solve(n>>1);
A=mul(A,A),B=mul(B,B);
if(n>>1&1)A=mul(A,a),B=mul(B,b);
return n&1?mul(mul(A,a)+mul(B,b),res):(mul(A+B,res)-mul(A,B)+P)%P;
}
signed main(){
// freopen("testdata.in","r",stdin);
int T=read();
while(T--){
n=read(),a=read(),b=read(),P=read(),A=B=1;
print((solve(n)+P)%P);
}
return Ot(),0;
}

P5137 polynomial(分治)的更多相关文章

  1. [洛谷P5137]polynomial

    题目大意:求:$$\sum\limits_{i=0}^na^{n-i}b^i\pmod{p}$$$T(T\leqslant10^5)$组数据,$a,b,n,p\leqslant10^{18}​$ 题解 ...

  2. HDU 6900 Residual Polynomial【分治 NTT】

    HDU 6900 Residual Polynomial  题意: 给出一个多项式\(f_1(x) = \sum_{i=0}^na_ix^i\) 对于任意\(i>=2\),满足\(f_i(x) ...

  3. 【洛谷4721】【模板】分治FFT(CDQ分治_NTT)

    题目: 洛谷 4721 分析: 我觉得这个 "分治 FFT " 不能算一种特殊的 FFT ,只是 CDQ 分治里套了个用 FFT (或 NTT)计算的过程,二者是并列关系而不是偏正 ...

  4. LG4721 【模板】分治 FFT

    P4721 [模板]分治 FFT 题目背景 也可用多项式求逆解决. 题目描述 给定长度为 $n-1$ 的数组 $g[1],g[2],..,g[n-1]$,求 $f[0],f[1],..,f[n-1]$ ...

  5. [bzoj2152][聪聪和可可] (点分治+概率)

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  6. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  7. [poj1741][tree] (树/点分治)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  8. 【教程】简易CDQ分治教程&学习笔记

    前言 辣鸡蒟蒻__stdcall终于会CDQ分治啦!       CDQ分治是我们处理各类问题的重要武器.它的优势在于可以顶替复杂的高级数据结构,而且常数比较小:缺点在于必须离线操作. CDQ分治的基 ...

  9. BZOJ 3262 陌上花开 ——CDQ分治

    [题目分析] 多维问题,我们可以按照其中一维排序,然后把这一维抽象的改为时间. 然后剩下两维,就像简单题那样,排序一维,树状数组一维,按照时间分治即可. 挺有套路的一种算法. 时间的抽象很巧妙. 同种 ...

随机推荐

  1. 《MySQL必知必会学习笔记》:子查询

    子查询 在開始了解子查询之前,首先做下准备工作,建立3个表, 一个是customers表,当中包含:客户名字.客户ID.客户Tel等. 一个是orders表,当中包含:订单号.客户ID.订单时间等. ...

  2. C++正则表达式笔记之wregex

    遍历所有匹配 #include <iostream> #include <regex> using namespace std; int main() { wstring ws ...

  3. UVA 10529 - Dumb Bones(概率+区间dp)

    UVA 10529 - Dumb Bones option=com_onlinejudge&Itemid=8&category=518&page=show_problem&am ...

  4. 【转】hibernate懒加载的问题,failed to lazily initialize a collection of role

    hibernate懒加载的问题,failed to lazily initialize a collection of role hibernate懒加载的问题,failed to lazily in ...

  5. Codeforces Round #363 (Div. 2) C. Vacations —— DP

    题目链接:http://codeforces.com/contest/699/problem/C 题解: 1.可知每天有三个状态:1.contest ,2.gym,3.rest. 2.所以设dp[i] ...

  6. .cxx_destruct crash

    开发过程中遇到 YXTBaseLabelCell .cxx_destruct崩溃,查了下,会在调用类的dealloc方法时调用cxx_destruct,于是看了下代码,找dealloc可能会崩溃的原因 ...

  7. this.triggerEvent()用法

    在对组件进行封装时 在当前页面想要获取组件中的某一状态,需要使用到this.triggerEvent(' ',{},{}),第一个参数是自定义事件名称,这个名称是在页面调用组件时bind的名称,第二个 ...

  8. android读取apk中已经存在的数据库信息

    在android数据库编程方面,大家有没有遇到过,我要从指定位置的已经存在的数据库来进行操作的问题.之前我尝试了很多方法都没有成功,后来找到了解决的方法.   下面说明下这段代码的意思,第一步先判断在 ...

  9. 提高scroll性能

    在DevTools中开始渲染,向下滑动一点点滚动条,然后停止滚动. 在结果中,注意frames总是在30ftps线上面,甚至都木有很接近69ftps线的(事实上帧执行的太缓慢以致于60ftps线在图上 ...

  10. 【应用】SVG饼状图

    <!DOCTYPE html> <html> <head> <title></title> </head> <body o ...