Problem:
Lucky Number
Problem
0 から
N
-1 までの数字があります。
M
+1日間のラッキーナンバーを以下の方法で選びたいと思います。 最初の日にラッキーナンバーをランダムに決めます。
i
日後のラッキーナンバーを
A
、（
i
+1）日後のラッキーナンバーを
B
とすると、
B
= (
A
+
j
) %
N
（ただし
j
は 0 ≤
j
<
N
かつ （
j
/
K
） が偶数となる全ての整数である )
で求められる
B
のうちのいずれかからランダムに決めます。ただし、
a
/
b
の結果は小数点以下を切り捨てた整数とし、
a
%
b
は
a
を
b
で割った余りとします。
また
K
は
N
/
K
が割り切れて、
N
/
K
が偶数となる数字であることが保証されます。
例えば 0,1,2,3 と数字があり、
K
= 1 のとき
0 の次のラッキーナンバーは 0 or 2
1 の次のラッキーナンバーは 1 or 3
2 の次のラッキーナンバーは 0 or 2
3 の次のラッキーナンバーは 1 or 3
となります。
次に
Q
個の質問が与えられます。 各質問の内容は以下のとおりです。
最初の日のラッキーナンバーが
a
のとき
b
日後のラッキーナンバーが
c
になるような
b
日後までのラッキーナンバーの選び方は何通りでしょうか？
選び方は膨大な数になると思われるので 1,000,000,007 で割った余りを求めてください。
例えば、
N
=4
K
=1 で、最初のラッキーナンバーが 0 で　3 日後のラッキーナンバーが 2 になるような組み合わせは
0 → 0 → 0 → 2
0 → 0 → 2 → 2
0 → 2 → 0 → 2
0 → 2 → 2 → 2
の４通りです。
Input
入力は以下の形式で与えられる。
N
M
K
Q
a
1
b
1
c
1
a
2
b
2
c
2
.
.
.
a
Q
b
Q
c
Q
Constraints
1 ≤
N
≤ 5000
1 ≤
M
≤ 5000
1 ≤
K
≤ 5000
N
/
K
が割り切れる
N
/
K
は偶数
1 ≤
Q
≤ 100000
0 ≤
a
i
<
N
( 1 ≤
i
≤
Q
)
0 ≤
b
i
≤
M
( 1 ≤
i
≤
Q
)
0 ≤
c
i
<
N
( 1 ≤
i
≤
Q
)
Output
各質問に対し、答えを求め 1,000,000,007 で割った余りを一行ずつ順番に出力してください。
Sample Input1
6 3 1 10
0 1 0
0 2 0
0 3 0
1 3 3
0 2 1
2 2 2
2 3 2
2 2 0
1 1 1
2 2 1
Sample Output1
1
3
9
9
0
3
9
3
1
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll N,M,K,Q;
ll dp[5001][5001];

int main(){
  cin>>N>>M>>K>>Q;
  dp[0][0]=1;
  rep(i,M){
    ll crtdp=0;
    rep(A,N){
      ll j=(N-A)%N;
      if((j/K)%2==0)(crtdp+=dp[i][A])%=mod;
    }
    rep(B,2*K){
      for(ll b=B;b<N;b+=2*K)dp[i+1][b]=crtdp;
      rep(j,N/K){
        ll a=(j*K+(B+1))%N;
        if(j%2==0) (crtdp+=dp[i][a])%=mod;
        else (crtdp+=mod-dp[i][a])%=mod;
      }
    }
  }

  while(Q--){
    ll a,b,c;
    cin>>a>>b>>c;
    c-=a;
    if(c<0)c+=N;
    cout<<dp[b][c]<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 1000
memory_kb: 198452
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int mod = 1e9+7;
const int N = 5005;

int dp[N][N]={};

int main(){
    int n,m,k,q;
    scanf(" %d %d %d %d", &n, &m, &k, &q);

    dp[0][0] = 1;
    rep(i,N-1){
        int val = 0;
        rep(j,n){
            if( (j/k)%2==0 ){
                int idx = (0-j+n)%n;
                (val += dp[i][idx]) %= mod;
            }
        }

        int B = 2*k;
        rep(j,B){
            for(int x=j; x<n; x+=B) dp[i+1][x] = val;

            rep(x,n/k){
                int idx = (j+1-x*k+n)%n;
                if(x%2==0) val = (val+dp[i][idx])%mod;
                else val = (val-dp[i][idx]+mod)%mod;
            }
        }
    }

    rep(i,q){
        int a,b,c;
        scanf(" %d %d %d", &a, &b, &c);
        int d = c-a;
        if(d<0) d+=n;
        printf("%d\n", dp[b][d]);
    }

    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 101040
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n,m,K,q;
ll dp[5005][5005];
ll tp[5005];

ll get(int f,int t){
	if(f>=0){
		return (tp[t]-tp[f]+MOD)%MOD;
	}else{
		return (tp[K*2]-tp[K*2+f]+tp[t]+MOD)%MOD;
	}
}

int main(void){
	scanf("%d%d%d%d",&n,&m,&K,&q);
	dp[0][0]=1;
	for(int i=0;i<=m;i++){
		memset(tp,0,sizeof(tp));
		for(int j=0;j<2*K;j++){
			for(int k=0;k<n/(2*K);k++){
				tp[j+1]+=dp[i][k*(2*K)+j];
				tp[j+1]%=MOD;
			}
		}
		for(int j=0;j<2*K;j++){
			tp[j+1]+=tp[j];
			tp[j+1]%=MOD;
		}
		for(int j=0;j<2*K;j++){
			for(int k=0;k<n/(2*K);k++){
				//printf("%d %d %d %lld\n",i,j,k,get(j,j+K));
				dp[i+1][k*(2*K)+j]=get(j-K+1,j+1);
				//printf("%d %d %d %d %lld\n",i+1,k*(2*K)+j,j-K+1,j+1,dp[i+1][k*(2*K)+j]);
			}
		}
	}
	for(int i=0;i<q;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		int dif=(c-a+n)%n;
		printf("%lld\n",dp[b][dif]);
	}
	return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 198808
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
const int MOD = 1e9+7;
int sm[10050];
int dp[5050][5050];
signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n,m,k,q;
  cin>>n>>m>>k>>q;
  memset(dp,0,sizeof(dp));
  dp[0][0]=1;
  for(int i=0;i<m;i++){
    memset(sm,0,sizeof(sm));
    for(int j=0;j<n;j++){
      int x=j%(2*k);
      sm[x]+=dp[i][j];
      sm[x]%=MOD;
      sm[2*k+x]+=dp[i][j];
      sm[2*k+x]%=MOD;
    }
    
    for(int j=1;j<4*k;j++) sm[j]=(sm[j]+sm[j-1])%MOD;
      
    for(int j=0;j<n;j++)
      dp[i+1][j]=(sm[2*k+(j%(2*k))]+MOD-sm[k+(j%(2*k))])%MOD;
  }
  
  for(int i=0;i<q;i++){
    int a,b,c;
    cin>>a>>b>>c;
    int d=(c-a+n)%n;
    cout<<dp[b][d]<<endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 102868
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
const int MOD = 1e9+7;
int sm[10050];
int dp[5050][5050];
signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n,m,k,q;
  cin>>n>>m>>k>>q;
  memset(dp,0,sizeof(dp));
  dp[0][0]=1;
  for(int i=0;i<m;i++){
    memset(sm,0,sizeof(sm));
    for(int j=0;j<n;j++){
      int x=j%(2*k);
      sm[x]+=dp[i][j];
      sm[x]%=MOD;
      sm[2*k+x]+=dp[i][j];
      sm[2*k+x]%=MOD;
    }
    
    for(int j=1;j<4*k;j++) sm[j]=(sm[j]+sm[j-1])%MOD;
      
    for(int j=0;j<n;j++)
      dp[i+1][j]=(sm[2*k+(j%(2*k))]+MOD-sm[k+(j%(2*k))])%MOD;
  }
  
  for(int i=0;i<q;i++){
    int a,b,c;
    cin>>a>>b>>c;
    int d=(c-a+n)%n;
    cout<<dp[b][d]<<endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 102868
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
#define MOD 1000000007

namespace FFT{
  using dbl = long double;
  
  struct num{
    dbl x,y;
    num(){x=y=0;}
    num(dbl x,dbl y):x(x),y(y){}
  };
  
  inline num operator+(num a,num b){
    return num(a.x+b.x,a.y+b.y);
  }
  inline num operator-(num a,num b){
    return num(a.x-b.x,a.y-b.y);
  }
  inline num operator*(num a,num b){
    return num(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);
  }
  inline num conj(num a){
    return num(a.x,-a.y);
  }
 
  int base=1;
  vector<num> rts={{0,0},{1,0}};
  vector<int> rev={0,1};
 
  const dbl PI=acosl(-1.0);
 
  void ensure_base(int nbase){
    if(nbase<=base) return;
 
    rev.resize(1<<nbase);
    for(int i=0;i<(1<<nbase);i++)
      rev[i]=(rev[i>>1]>>1)+((i&1)<<(nbase-1));
 
    rts.resize(1<<nbase);
    while(base<nbase){
      dbl angle=2*PI/(1<<(base+1));
      for(int i=1<<(base-1);i<(1<<base);i++){
	rts[i<<1]=rts[i];
	dbl angle_i=angle*(2*i+1-(1<<base));
	rts[(i<<1)+1]=num(cos(angle_i),sin(angle_i));
      }
      base++;
    }
  }
 
  void fft(vector<num> &a,int n=-1){
    if(n==-1) n=a.size();
    assert((n&(n-1))==0);
 
    int zeros=__builtin_ctz(n);
    ensure_base(zeros);
    int shift=base-zeros;
    for(int i=0;i<n;i++)
      if(i<(rev[i]>>shift))
	swap(a[i],a[rev[i]>>shift]);
 
    for(int k=1;k<n;k<<=1){
      for(int i=0;i<n;i+=2*k){
	for(int j=0;j<k;j++){
	  num z=a[i+j+k]*rts[j+k];
	  a[i+j+k]=a[i+j]-z;
	  a[i+j]=a[i+j]+z;
	}
      }
    }
  }
 
  vector<num> fa;
 
  vector<Int> multiply(vector<int> &a,vector<int> &b){
    int need=a.size()+b.size()-1;
    int nbase=0;
    while((1<<nbase)<need) nbase++;
    ensure_base(nbase);
 
    int sz=1<<nbase;
    if(sz>(int)fa.size()) fa.resize(sz);
    for(int i=0;i<sz;i++){
      int x=(i<(int)a.size()?a[i]:0);
      int y=(i<(int)b.size()?b[i]:0);
      fa[i]=num(x,y);
    }
    fft(fa,sz);
 
    num r(0,-0.25/sz);
    for(int i=0;i<=(sz>>1);i++){
      int j=(sz-i)&(sz-1);
      num z=(fa[j]*fa[j]-conj(fa[i]*fa[i]))*r;
      if(i!=j)
	fa[j]=(fa[i]*fa[i]-conj(fa[j]*fa[j]))*r;
      fa[i]=z;
    }
    fft(fa,sz);
 
    vector<Int> res(need);
    for(int i=0;i<need;i++)
      res[i]=fa[i].x+0.5;
    
    return res;
  }
  
};

signed main(){  
  cin.tie(0);
  ios::sync_with_stdio(0);
  
  Int n,m,k,q;
  cin>>n>>m>>k>>q;

  vector<int> v(n,0);
  for(int j=0;j<n;j++) v[j]=((j/k)%2==0); 
  vector<int> dp(n,0);
  dp[0]=1;
  int p=0;
  auto calc=[&](){
    p++;
    auto nx=FFT::multiply(dp,v);
    for(int i=0;i<n;i++) dp[i]=0;
    for(int i=0;i<(int)nx.size();i++) dp[i%n]+=nx[i];
    for(int i=0;i<n;i++) dp[i]%=MOD;
  };
  
  using T = tuple<int, int, int>;
  vector<T> ans(q);
  for(int i=0;i<q;i++){
    int a,b,c;
    cin>>a>>b>>c;
    ans[i]=T(b,(c+n-a)%n,i);
  }
  sort(ans.begin(),ans.end());
  vector<int> pri(q);
  for(int i=0;i<q;i++){
    int b,l,idx;
    tie(b,l,idx)=ans[i];
    while(p<b) calc();
    pri[idx]=dp[l];
  }
  for(int i=0;i<q;i++) cout<<pri[i]<<endl;
  return 0;
}
accuracy: 0.14285714285714285
time_ms: 160
memory_kb: 4452
final_rank: 20

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
    Info(int arg_start,int arg_end){
        start = arg_start;
        end = arg_end;
    }
    int start,end;
};

ll dp[5001][5001],work[16000];


int main(){

    int N,M,K,Q;

    scanf("%d %d %d %d",&N,&M,&K,&Q);

    int LIMIT = 0;
    while(LIMIT < N)LIMIT += 2*K;

    vector<Info> V;

    for(int day = 0; day <= M; day++){
        for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
    }

    dp[0][0] = 1;

    for(int day = 0; day <= M-1; day++){

        for(int i = 0; i < 2*N; i++)work[i] = 0;

        for(int num = 0; num <= N-1; num++){

            if(dp[day][num] == 0)continue;

            work[num] += dp[day][num];
            work[num+K] -= dp[day][num];
            work[num+LIMIT] -= dp[day][num];
            work[num+LIMIT+K] += dp[day][num];
        }

        for(int i = 1; i < 2*N; i++){
            work[i] += work[i-1];
        }

        for(int i = 0; i < 2*N; i++){
            if(i+2*K < 2*N){
                work[i+2*K] += work[i];
            }else{
                break;
            }
        }

        dp[day+1][0] += work[0]%MOD;
        dp[day+1][0] %= MOD;

        for(int i = 1; i < 2*N; i++){
            dp[day+1][i%N] += work[i]%MOD;
            dp[day+1][i%N] %= MOD;
        }
    }

    int a,b,c;

    for(int loop = 0; loop < Q; loop++){
        scanf("%d %d %d",&a,&b,&c);

        c = (c-a+N)%N;

        printf("%lld\n",dp[b][c]%MOD);
    }

    return 0;
}
accuracy: 1
time_ms: 440
memory_kb: 198684
final_rank: 9

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[16000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	int LIMIT = 0;
	while(LIMIT < N)LIMIT += 2*K;

	vector<Info> V;

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){

		for(int i = 0; i < 2*N; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){

			if(dp[day][num] == 0)continue;

			work[num] += dp[day][num];
			work[num+K] -= dp[day][num];
			work[num+LIMIT] -= dp[day][num];
			work[num+LIMIT+K] += dp[day][num];
		}

		for(int i = 1; i < 2*N; i++){
			work[i] += work[i-1];
		}

		for(int i = 0; i < 2*N; i++){
			if(i+2*K < 2*N){
				work[i+2*K] += work[i];
			}else{
				break;
			}
		}

		for(int i = 0; i < 2*N; i++){
			dp[day+1][i%N] += work[i]%MOD;
		}
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 1
time_ms: 400
memory_kb: 198692
final_rank: 7

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[16000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	int LIMIT = 0;
	while(LIMIT < N)LIMIT += 2*K;

	vector<Info> V;

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){

		for(int i = 0; i < 2*N; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){

			if(dp[day][num] == 0)continue;

			work[num] += dp[day][num];
			work[num+K] -= dp[day][num];
			if(num+LIMIT < 2*N)work[num+LIMIT] -= dp[day][num];
			if(num+LIMIT+K < 2*N)work[num+LIMIT+K] += dp[day][num];
		}

		for(int i = 1; i < 2*N; i++){
			work[i] += work[i-1];
		}

		for(int i = 0; i < 2*N; i++){
			if(i+2*K < 2*N){
				work[i+2*K] += work[i];
			}else{
				break;
			}
		}

		for(int i = 0; i < N; i++){
			dp[day+1][i] = (work[i]+work[i+N])%MOD;
		}
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 198680
final_rank: 6

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[16000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	vector<Info> V;

	for(int i = 0; i+K-1 < N; i += 2*K){
		V.push_back(Info(i,i+K-1));
	}

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){
		for(int i = 0; i < 3*N+2; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){
			if(dp[day][num] == 0)continue;

			for(int i = 0; i < V.size(); i++){
				work[num+V[i].start] += dp[day][num];
				work[num+V[i].end+1] -= dp[day][num];
			}
		}

		dp[day+1][0] += work[0]%MOD;
		dp[day+1][0] %= MOD;

		for(int i = 1; i < 3*N+2; i++){
			work[i] += work[i-1];
			work[i] %= MOD;
			dp[day+1][i%N] += work[i];
			dp[day+1][i%N] %= MOD;
		}
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 0.14285714285714285
time_ms: 40
memory_kb: 7100
final_rank: 13

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[16000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	vector<Info> V;

	for(int i = 0; i+K-1 < N; i += 2*K){
		V.push_back(Info(i,i+K-1));
	}

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){
		//for(int i = 0; i < 3*N+2; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){
			if(dp[day][num] == 0)continue;

			for(int i = 0; i < V.size(); i++){
				for(int k = V[i].start; k <= V[i].end; k++){
					dp[day+1][(num+k)%N] += dp[day][num];
				}
				/*work[num+V[i].start] += dp[day][num];
				work[num+V[i].end+1] -= dp[day][num];*/
			}
		}

		/*dp[day+1][0] += work[0]%MOD;
		dp[day+1][0] %= MOD;

		for(int i = 1; i < 3*N+2; i++){
			work[i] += work[i-1];
			work[i] %= MOD;
			dp[day+1][i%N] += work[i];
			dp[day+1][i%N] %= MOD;
		}*/
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 0.14285714285714285
time_ms: 50
memory_kb: 7168
final_rank: 18

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[16000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	vector<Info> V;

	for(int i = 0; i < N; i += 2*K){
		V.push_back(Info(i,min(i+K-1,N-K-1)));
	}

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){
		for(int i = 0; i < 3*N+2; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){
			if(dp[day][num] == 0)continue;

			for(int i = 0; i < V.size(); i++){
				for(int k = V[i].start; k <= V[i].end; k++){
					dp[day+1][(num+k)%N] += dp[day][num];
				}
				/*work[num+V[i].start] += dp[day][num];
				work[num+V[i].end+1] -= dp[day][num];*/
			}
		}

		/*dp[day+1][0] += work[0]%MOD;
		dp[day+1][0] %= MOD;

		for(int i = 1; i < 3*N+2; i++){
			work[i] += work[i-1];
			work[i] %= MOD;
			dp[day+1][i%N] += work[i];
			dp[day+1][i%N] %= MOD;
		}*/
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 0.14285714285714285
time_ms: 40
memory_kb: 7160
final_rank: 16

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[16000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	vector<Info> V;

	for(int i = 0; i < N; i += 2*K){
		V.push_back(Info(i,i+K-1));
	}

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){
		for(int i = 0; i < 3*N+2; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){
			if(dp[day][num] == 0)continue;

			for(int i = 0; i < V.size(); i++){
				for(int k = V[i].start; k <= V[i].end; k++){
					dp[day+1][(num+k)%N] += dp[day][num];
				}
				/*work[num+V[i].start] += dp[day][num];
				work[num+V[i].end+1] -= dp[day][num];*/
			}
		}

		/*dp[day+1][0] += work[0]%MOD;
		dp[day+1][0] %= MOD;

		for(int i = 1; i < 3*N+2; i++){
			work[i] += work[i-1];
			work[i] %= MOD;
			dp[day+1][i%N] += work[i];
			dp[day+1][i%N] %= MOD;
		}*/
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 0.14285714285714285
time_ms: 50
memory_kb: 7160
final_rank: 17

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[16000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	vector<Info> V;

	for(int i = 0; i < N; i += 2*K){
		V.push_back(Info(i,i+K-1));
	}

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){
		for(int i = 0; i < 3*N+2; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){
			if(dp[day][num] == 0)continue;

			for(int i = 0; i < V.size(); i++){
				work[num+V[i].start] += dp[day][num];
				work[num+V[i].end+1] -= dp[day][num];
			}
		}

		dp[day+1][0] += work[0]%MOD;
		dp[day+1][0] %= MOD;

		for(int i = 1; i < 3*N+2; i++){
			work[i] += work[i-1];
			work[i] %= MOD;
			dp[day+1][i%N] += work[i];
			dp[day+1][i%N] %= MOD;
		}
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 0.14285714285714285
time_ms: 40
memory_kb: 7132
final_rank: 15

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[12000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	vector<Info> V;

	for(int i = 0; i < N; i += 2*K){
		V.push_back(Info(i,i+K-1));
	}

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){
		for(int i = 0; i < 2*N+2; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){
			if(dp[day][num] == 0)continue;

			for(int i = 0; i < V.size(); i++){
				work[num+V[i].start] += dp[day][num];
				work[num+V[i].end+1] -= dp[day][num];
			}
		}

		dp[day+1][0] += work[0]%MOD;
		dp[day+1][0] %= MOD;

		for(int i = 1; i < 2*N+2; i++){
			work[i] += work[i-1];
			work[i] %= MOD;
			dp[day+1][i%N] += work[i];
			dp[day+1][i%N] %= MOD;
		}
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 0.14285714285714285
time_ms: 50
memory_kb: 7180
final_rank: 19

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[12000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	vector<Info> V;

	for(int i = 0; i < N; i += 2*K){
		V.push_back(Info(i,i+K-1));
	}

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){
		for(int i = 0; i < 2*N+2; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){
			if(dp[day][num] == 0)continue;

			for(int i = 0; i < V.size(); i++){
				work[num+V[i].start] += dp[day][num];
				work[num+V[i].end+1] -= dp[day][num];
			}
		}

		dp[day+1][0] += work[0]%MOD;

		for(int i = 1; i < 2*N+2; i++){
			work[i] += work[i-1];
			work[i] %= MOD;
			dp[day+1][i%N] += work[i];
			dp[day+1][i%N] %= MOD;
		}
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]%MOD);
	}

	return 0;
}
accuracy: 0.14285714285714285
time_ms: 40
memory_kb: 7116
final_rank: 14

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(int arg_start,int arg_end){
		start = arg_start;
		end = arg_end;
	}
	int start,end;
};

ll dp[5001][5001],work[12000];


int main(){

	int N,M,K,Q;

	scanf("%d %d %d %d",&N,&M,&K,&Q);

	vector<Info> V;

	for(int i = 0; i < N; i += 2*K){
		V.push_back(Info(i,i+K-1));
	}

	for(int day = 0; day <= M; day++){
		for(int num = 0; num <= N-1; num++)dp[day][num] = 0;
	}

	dp[0][0] = 1;

	for(int day = 0; day <= M-1; day++){
		for(int i = 0; i < 2*N+2; i++)work[i] = 0;

		for(int num = 0; num <= N-1; num++){
			if(dp[day][num] == 0)continue;

			for(int i = 0; i < V.size(); i++){
				work[num+V[i].start] += dp[day][num];
				work[num+V[i].end+1] -= dp[day][num];
			}
		}

		dp[day+1][0] += work[0]%MOD;

		for(int i = 1; i < 2*N+2; i++){
			work[i] += work[i-1];
			work[i] %= MOD;
			dp[day+1][i%N] += work[i];
		}
	}

	int a,b,c;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %d",&a,&b,&c);

		c = (c-a+N)%N;

		printf("%lld\n",dp[b][c]);
	}

	return 0;
}
accuracy: 0.14285714285714285
time_ms: 40
memory_kb: 7084
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[5001][5001];
 
int main() {
  ll n,m,k,q,mod=1000000007;
  cin >> n >> m >> k >> q;
  dp[0][0]=1;
  for(int i=0; i<n; i++) {
    if(i/k%2==0) dp[1][i]=1;
  }
  ll x=n/k/2,N=k*2;
  for(int i=1; i<m; i++) {
    ll sum=0;
    for(int j=N-k; j<N; j++) sum+=dp[i][j];
    for(int j=0; j<n; j++) {
      sum-=dp[i][(j-k+N)%N];
      sum+=dp[i][j];
      dp[i+1][j]=sum*x;
      dp[i+1][j]%=mod;
    }
  }
  while(q--) {
    int a,b,c;
    cin >> a >> b >> c;
    cout << dp[b][(c-a+n)%n] << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 430
memory_kb: 198456
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
 
 
int mod = 1000000007;
long long dp[5010][5010];
 
int main(){
    vector<int> mov;
    int N,K,M,Q;
    cin >> N >> M >> K >> Q;
    for(int i = 0 ; i < N ; i++){
        if( (i / K) % 2 == 0 ){
			mov.push_back(i);
            //cerr << i << endl;
        }
    }
    dp[0][0] = 1;
    for(int i = 0 ; i <= 5000 ; i++){
		int tot[5010] = {};
		
		// for(int k = 0 ; k < N ; k++){
			// printf("%3d",dp[i][k]);
			// cout << " ";
		// }
		// cout << endl;
		// for(int k = 0 ; k < N ; k++) cout << (k/K%2==0?"  ^":"   ") << " ";
		// cout << endl;
		
		for(int k = 0 ; k < mov.size() ; k++){
			tot[0] += dp[i][mov[k]%N];
			tot[0] %= mod;
		}
		
		for(int j = 1 ; j < 2*K ; j++){
			tot[j] = tot[j-1];
			int c = -1;
			for(int k = 0 ; k < N ; k += K){
				tot[j] += c*dp[i][(k+j-1)%N];
				tot[j] %= mod;
				c *= -1;
			}
		}
		
		//reverse(tot,tot+2*K);
		// for(int j = 0 ; j < 2*K ; j++) cout << tot[j] << " " ;
		// cout << endl;
		
		for(int j = 0 ; j < N ; j++){
			dp[i+1][j] += tot[(K+1+j)%(2*K)];
			dp[i+1][j] %= mod;
		}
    }
    for(int i = 0 ; i < Q ; i++){
        int a,b,c;
        cin >> a >> b >> c;
        int d = ((c-a)%N+N)%N;
        cout << (dp[b][d]%mod+mod)%mod << endl;
    }
	// for(int i = 0 ; i < 15 ; i++){
		// for(int j = 0 ; j < 15 ; j++){
			// printf("%10d ",(dp[i][j]+mod)%mod);
		// }
		// cout << endl;
	// }
     
}
accuracy: 1
time_ms: 900
memory_kb: 197012
final_rank: 10

code_snippet: #include <algorithm>
#include <vector>
#include <string>
#include <iostream>
namespace lc {
template <int MOD>
class ModulusInteger {
public:
	typedef ModulusInteger<MOD> self_type;
private:
	int m_value;
	static self_type unsafe_construct(int x){
		self_type y;
		y.m_value = x;
		return y;
	}
public:
	ModulusInteger()
		: m_value(0)
	{ }
	ModulusInteger(int x)
		: m_value(x % MOD)
	{
		if(m_value < 0){ m_value += MOD; }
	}
	int operator*() const { return m_value; }
	self_type &operator=(const self_type &x){
		m_value = x.m_value;
		return *this;
	}
	self_type operator+(const self_type &x) const {
		const int y = m_value + x.m_value;
		return unsafe_construct(y >= MOD ? y - MOD : y);
	}
	self_type operator-(const self_type &x) const {
		const int y = m_value - x.m_value;
		return unsafe_construct(y < 0 ? y + MOD : y);
	}
	self_type operator*(const self_type &x) const {
		return unsafe_construct(
			static_cast<long long>(m_value) * x.m_value % MOD);
	}
	self_type &operator+=(const self_type &x){ return (*this = *this + x); }
	self_type &operator-=(const self_type &x){ return (*this = *this - x); }
	self_type &operator*=(const self_type &x){ return (*this = *this * x); }
};
template <int MOD>
std::ostream& operator<<(std::ostream &os, const ModulusInteger<MOD> &x){
	os << *x;
    return os;
}
}
using namespace std;
typedef lc::ModulusInteger<1000000007> mint;
static mint dp[5001][5001];
int main(){
	ios_base::sync_with_stdio(false);
	int n, m, k, q;
	cin >> n >> m >> k >> q;
	dp[0][0] = 1;
	for(int i = 0; i < m; ++i){
		for(int j = 0; j < k; ++j){
			const mint t = dp[i][j];
			dp[i + 1][j]     += t;
			dp[i + 1][j + k] -= t;
		}
		for(int j = k; j < k + k; ++j){
			const mint t = dp[i][j];
			dp[i + 1][j]     += t;
			dp[i + 1][k + k] -= t;
			dp[i + 1][0]     += t;
			dp[i + 1][j - k] -= t;
		}
		for(int j = 0; j < k + k; ++j){
			dp[i + 1][j + 1] += dp[i + 1][j];
		}
	}
	for(int i = 0; i < q; ++i){
		int a, b, c;
		cin >> a >> b >> c;
		if(b == 0){
			cout << (a == c ? 1 : 0) << endl;
		}else{
			a %= (2 * k);
			c %= (2 * k);
			auto answer = dp[b][(c - a + 2 * k) % (2 * k)];
			if(b > 1){
				mint x = n / (k * 2), z = 1;
				--b;
				while(b > 0){
					if(b & 1){ z *= x; }
					x *= x;
					b >>= 1;
				}
				answer *= z;
			}
			cout << answer << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 98892
final_rank: 1