結果
| 問題 | No.612 Move on grid |
| コンテスト | |
| ユーザー |
夕叢霧香(ゆうむらきりか)
|
| 提出日時 | 2017-12-12 02:37:05 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,447 bytes |
| 記録 | |
| コンパイル時間 | 2,308 ms |
| コンパイル使用メモリ | 77,872 KB |
| 実行使用メモリ | 118,772 KB |
| 最終ジャッジ日時 | 2024-11-30 21:25:15 |
| 合計ジャッジ時間 | 57,929 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 2 TLE * 15 |
ソースコード
import java.io.*;
import java.util.*;
class Main {
static final long MOD=1000000007;
static final int N=2000;
static long[] fact;
static long[] invfact;
static void initialize() {
fact = new long[N];
invfact = new long[N];
fact[0] = invfact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = (i * fact[i - 1]) % MOD;
invfact[i] = powerMod(fact[i], MOD - 2);
}
}
static long powerMod(long x, long exponent) {
long prod = 1;
for (int i = 63; i >= 0; --i) {
prod = (prod * prod) % MOD;
if ((exponent & 1L << i) != 0) {
prod = (prod * x) % MOD;
}
}
return prod;
}
static long comb(int x, int y) {
if (x < 0) {
return 0;
}
if (y < 0 || y > x) {
return 0;
}
long r= (fact[x] * powerMod((fact[x - y] * fact[y]) % MOD, MOD - 2)) % MOD;
return r;
}
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t=sc.nextInt();
int[] a=new int[3];
for(int i=0;i<3;++i)a[i]=sc.nextInt();
int d=sc.nextInt();
int e=sc.nextInt();
if(a[2]<0){
for(int i=0;i<3;++i)a[i]=-a[i];
int tmp=d;
d=-e;
e=-tmp;
}
initialize();
long[][]prec=new long[t+1][2*t+2];
for(int i=0;i<=t;++i){
for(int z=-t;z<=t;++z){
int az=Math.abs(z);
if((az+i)%2!=0)continue;
int j=(i-z)/2;
prec[i][z+t+1]=j>=0&&j+z>=0?invfact[j]*invfact[j+z]%MOD:0;
}
for(int j=0;j<2*t+1;++j)
prec[i][j+1]=(prec[i][j+1]+prec[i][j])%MOD;
}
long wa=0;
for(int x=-t;x<=t;++x){
int ax=Math.abs(x);
int tx=t-ax;
for(int y=-tx;y<=tx;++y){
int ay=Math.abs(y);
int ty=tx-ay;
long[]d2=new long[t+1];
for(int i=ax+ay;i<=t;++i){
if((i+ax+ay)%2==0){
d2[i]=comb(i,(i-ax-ay)/2)*comb(i,(i-ax+ay)/2)%MOD;
d2[i]=d2[i]*invfact[i]%MOD;
}
}
int zMin=-ty,zMax=ty;
if(a[2]!=0){
zMin=Math.max(zMin,(d-a[0]*x-a[1]*y+100000*a[2]+a[2]-1)/a[2]-100000);
zMax=Math.min(zMax,(e-a[0]*x-a[1]*y+100000*a[2])/a[2]-100000);
}else if(d-a[0]*x-a[1]*y>0||e-a[0]*x-a[1]*y<0)
continue;
if(zMin>zMax)continue;
//System.err.println("x = "+x+" y = "+y);
//System.err.println("zMin = "+zMin+" zMax = "+zMax);
//System.err.println(Arrays.toString(d2));
long com=fact[t];
long tmp=0,tmp2=0;
for(int i=(t+ax+ay)%2;i<=t;i+=2){
long adden=prec[i][t+zMax+1]-prec[i][t+zMin]+MOD;
adden%=MOD;
adden=adden*d2[t-i]%MOD;
tmp=(tmp+adden)%MOD;
}
wa=(wa+com*tmp)%MOD;
}
}
out.println(wa);
out.close();
}
// http://codeforces.com/blog/entry/7018
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
夕叢霧香(ゆうむらきりか)