fork(1) download
  1. def itOne(n,d,m,i):
  2. return (n*m+i) if n%d else n//d
  3.  
  4. def itFull(n,d,m,i):
  5. ni=n
  6. cs=[ni]
  7. while ni not in cs[:-1] and ni<=10**9 and len(cs)<1000:
  8. ni=itOne(ni,d,m,i)
  9. cs.append(ni)
  10. return cs
  11.  
  12. strin=input()
  13. d,m,i=map(int,strin.split())
  14.  
  15. s=[itFull(n,d,m,i) for n in range(1,1001)]
  16. r=[]
  17.  
  18. for se in s:
  19. if se[-1]>10**9 or len(se)>=1000:
  20. r+=[(0,)]
  21. else:
  22. i=se.index(se[-1])
  23. cl=se[i:-1]
  24. ci=(cl.index(min(cl))+1)%len(cl)
  25. cu=cl[ci:]+cl[:ci]
  26. r+=[tuple(cu)]
  27.  
  28. rs=sorted(set(r),key=lambda l:l[-1])
  29.  
  30. for re in rs:
  31. print('LOOP',*re) if re[-1]>0 else print('DIVERGENT')
  32.  
Success #stdin #stdout 0.68s 9200KB
stdin
6 9 9
stdout
DIVERGENT
LOOP 18 3 36 6 1
LOOP 27 252 42 7 72 12 2
LOOP 45 414 69 630 105 954 159 1440 240 40 369 3330 555 5004 834 139 1260 210 35 324 54 9 90 15 144 24 4
LOOP 81 738 123 1116 186 31 288 48 8
LOOP 99 900 150 25 234 39 360 60 10
LOOP 126 21 198 33 306 51 468 78 13