Codechef:Fibonacci Number/FN(二次剩余+bsgs)
题面
前置芝士
\(bsgs\),\(Cipolla\)
题解
因为题目保证\(p\bmod 10\)是完全平方数,也就是说\(p\bmod 5\)等于\(1\)或\(-1\),即\(5\)是模\(p\)的二次剩余(法老讲过,我忘了为啥了……)
然后我们需要用\(Cipolla\)求出\(c=\sqrt{5}\),并记\(p={1+c\over 2}\)
用斐波那契数列的通项公式代入,方程可以变为
\]
解得
\]
我们枚举一下\(n\)的奇偶性,开根可以用\(Cipolla\)处理,然后用\(bsgs\)解得\(n\)就可以了
//minamoto
#include<bits/stdc++.h>
#define R register
#define inf 0x7fffffff
#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)
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
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(R 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 P;
inline int add(R int x,R int y){return 0ll+x+y>=P?0ll+x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
R int res=1;
for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
return res;
}
int w,a;
struct cp{
int x,y;
inline cp(R int _x,R int _y):x(_x),y(_y){}
inline cp operator *(const cp &b)const{
return cp(add(mul(x,b.x),mul(w,mul(y,b.y))),add(mul(x,b.y),mul(y,b.x)));
}
};
int ksm(R cp x,R int y){
R cp res(1,0);
for(;y;y>>=1,x=x*x)if(y&1)res=res*x;
return res.x;
}
int Sqrt(int x){
if(!x)return 0;
if(ksm(x,(P-1)>>1)==P-1)return -1;
while(true){
a=mul(rand(),rand()),w=dec(mul(a,a),x);
if(ksm(w,(P-1)>>1)==P-1)return ksm(cp(a,1),(P+1)>>1);
}
}
const int N=262144;
struct Hash{
struct eg{int v,nx,w;}e[N];int head[N],tot;
inline void clr(){memset(head,0,sizeof(head)),tot=0;}
inline void add(R int v,R int w){e[++tot]={v,head[v&262143],w},head[v&262143]=tot;}
int query(int x){
go(x&262143)if(v==x)return e[i].w;
return -1;
}
}mp[2];
int bsgs(int x,int v,int sgn){
int m=sqrt(P)+1;mp[0].clr(),mp[1].clr();
for(R int i=1,res=mul(v,x);i<=m;++i,res=mul(res,x))mp[i&1].add(res,i);
for(R int i=1,tmp=ksm(x,m),res=tmp;i<=m;++i,res=mul(res,tmp))
if(mp[(i*m)&1^sgn].query(res)!=-1)return i*m-mp[(i*m)&1^sgn].query(res);
return inf;
}
int c,s,p,inv2,res,rt;
int main(){
srand(time(NULL));
// freopen("testdata.in","r",stdin);
for(int T=read();T;--T){
c=read(),P=read(),s=Sqrt(5),inv2=(P+1)>>1,p=mul(s+1,inv2),c=mul(c,s);
res=inf;
rt=Sqrt((1ll*c*c+4)%P);
if(rt!=-1){
cmin(res,bsgs(p,mul(add(c,rt),inv2),0)),
cmin(res,bsgs(p,mul(dec(c,rt),inv2),0));
}
rt=Sqrt((1ll*c*c+P-4)%P);
if(rt!=-1){
cmin(res,bsgs(p,mul(add(c,rt),inv2),1)),
cmin(res,bsgs(p,mul(dec(c,rt),inv2),1));
}
printf("%d\n",res==inf?-1:res);
}
return 0;
}
Codechef:Fibonacci Number/FN(二次剩余+bsgs)的更多相关文章
- Codechef:Fibonacci Number/FN——求通项+二次剩余+bsgs
题意 定义 $F_n$ 为 $$F_n = \left\{\begin{matrix}0, n=0\\ 1, n=1 \\F_{n-1} + F_{n-2}, n > 1\end{matrix} ...
- Fibonacci number
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- fibonacci number & fibonacci sequence
fibonacci number & fibonacci sequence https://www.mathsisfun.com/numbers/fibonacci-sequence.html ...
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- [UCSD白板题] The Last Digit of a Large Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- [UCSD白板题 ]Small Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)
根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...
- 求四百万以内Fibonacci(number)数列偶数结果的总和
又对啦...开心~~~~ 只是代码可能不符合PEP标准什么的... Each new term in the Fibonacci sequence is generated by adding the ...
- Algorithms - Fibonacci Number
斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...
随机推荐
- gen_projective_mosaic(Halcon例子)
* This example program shows how several images of a PCB can be combined * into a large mosaic image ...
- 向ArcGIS的ToolBarControl中添加任意的windows组建的方法[转]
向ArcGIS的ToolBarControl中添加任意的windows组建的方法[转] Link: http://www.cnblogs.com/mymhj/archive/2012/10/12/27 ...
- JSP中内置对象pageContent的使用
public class TestPageContext { public void getSomething(PageContext page){ ServletRequest request = ...
- maven使用阿里云maven库
在maven\conf\settings.xml中的mirrors添加 <mirror> <id>nexus-aliyun</id> <name>Nex ...
- Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点.比如345表示为链表5->4->3.而我们需要做的就是将两条链表代表的 ...
- 查看http的并发请求数及其TCP连接状态
统计80端口的连接数据 netstat -nat | grep -i "80" | wc -l 统计httpd协议连接数 ps -ef | grep httpd | wc -l 统 ...
- IPMI (Intelligent Platform Management Interface)
4.3. ipmitool - utility for controlling IPMI-enabled devices 4.3.1. ipmitool 4.3.1.1. ubuntu 确定硬件是否支 ...
- How to Restart Qt Application
How to restart QtApplication As we know, Restarting Application means to exit current application, t ...
- Golang 字符编码
需要添加的库 go get code.google.com/p/go.text/encoding go get code.google.com/p/go.text/transform 两个转码函数 i ...
- 在Qt(C++)中使用QThread实现多线程
1. 引言 多线程对于需要处理耗时任务的应用很有用,一方面响应用户操作.更新界面显示,另一方面在"后台"进行耗时操作,比如大量运算.复制大文件.网络传输等. 使用Qt框架开发应用程 ...