We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Mathematics
- Number Theory
- Period
- Discussions
Period
Period
Sort by
recency
|
6 Discussions
|
Please Login in order to post a comment
Looking at the leaderboard (language=Python3) I find that nearly everyone submits the same solution (the one that TChalla copied below).
Could anyone help me understand, why?
(what sense does this make, what is the personal advantage?)
Python3 solution
The editorial seems to be assuming that m is a prime number. (Although I experimentally solved for non-prime cases)
For people who already solved it, the idea used here is useful for finding period in many other similar recurrent sequences, such as Fibonacci numbers. Notice that, for any such sequence modulo p, the order is at most p^2. So first you need to find M such that the sequence surely repeats after a[M]. And if it is periodic, then you will also see that the order is a divisor of this M
Hi, I had a python solution to the problem based on baby-step giant-step algorithm to find discrete log but I got TLE, if someone can help me to optimize the solution, thanks in advance.
""" Here I show how to find a discrete log using baby step big step algorithms, Code """
def gcd(a,b): if a%b==0:return b,0,1 r,x,y=gcd(b,a%b) return r,y,x-(a/b)*y
def inv(a,b,m): k=(a**2-5*b**2)%m g,k,_=gcd(k,m) k%=m if g!=1:return None return ((a*k)%m,(-b*k)%m)
def pow(x,n,one,mul):
import math
def dLog(x,y,one,mul,inv,group_size): step=int(math.sqrt(group_size))+1
def solve(a,b,m):
T=input()
for i in range(T): a,b,m=map(int,raw_input().strip().split()) print solve(a,b,m)