1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
char IO; template <class T=int> T rd(){ T s=0; int f=0; while(!isdigit(IO=getchar())) if(IO=='-') f=1; do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return f?-s:s; }
const int N=1e6+10;
int t,a,c,m; int fx[N],fy[N];
ll gcd(ll a,ll b){ return b==0?a:gcd(b,a%b); }
ll D2(ll n){ return n*(n+1)/2; } ll F(ll a,ll b,ll c,ll n){ if(n<0) return 0; if(a==0) return (b/c)*(n+1); if(a>=c || b>=c) { ll ans=F(a%c,b%c,c,n)+D2(n)*(a/c)+(n+1)*(b/c); return ans; } ll t=(a*n+b)/c; ll ans=t*n-F(c,-b+c-1,a,t-1); return ans; }
ll CalcMod(int a,int b,int m,int n){ return F(a,b,m,n)-F(a,b,m*2,n)*2; }
ll Calc(int a,int b,int n){ if(~m&1){ a%=2,b%=2; if(a==0) return b*(n+1); return (n+1+b)/2; } else { int c=a%2,d=b%2; if(c==0) { if(d==0) return CalcMod(a,b,m,n); else return (n+1)-CalcMod(a,b,m,n); } else { ll ans=0;
ll t=CalcMod(a*2,b,m,n/2); if(d) t=n/2+1-t; ans+=t;
b+=a; n--; if(n<0) return ans; t=CalcMod(a*2,b,m,n/2); if(!d) t=n/2+1-t; ans+=t; return ans; } } }
int main(){ rep(kase,1,rd()) { t=rd(),a=rd(),c=rd(),m=rd(); fx[0]=1,fy[0]=0; rep(i,1,t) fx[i]=1ll*fx[i-1]*a%m,fy[i]=(1ll*fy[i-1]*a+c)%m; ll ans=0; rep(i,0,t) { if(i==0) { ans+=Calc(fx[i]*2%m,fy[i],t); } else { ll tmp=(1ll*i*fx[i]+fy[i])%m; ans+=Calc(fx[i]*2%m,tmp,t-i)*2; } } ll tmp=1ll*(t+1)*(t+1); ans=tmp-ans; ll g=gcd(tmp,ans); printf("%lld/%lld\n",ans/g,tmp/g); } }
|