結果

問題 No.1892 Extended Fib Series
ユーザー watarimaycry2watarimaycry2
提出日時 2022-08-09 16:09:10
言語 Java21
(openjdk 21)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 9,365 bytes
コンパイル時間 4,910 ms
コンパイル使用メモリ 94,036 KB
実行使用メモリ 62,960 KB
最終ジャッジ日時 2023-10-20 01:18:32
合計ジャッジ時間 10,693 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
57,192 KB
testcase_01 AC 182 ms
62,960 KB
testcase_02 AC 139 ms
59,676 KB
testcase_03 AC 102 ms
58,120 KB
testcase_04 AC 151 ms
60,552 KB
testcase_05 AC 87 ms
54,372 KB
testcase_06 AC 192 ms
62,656 KB
testcase_07 AC 165 ms
62,440 KB
testcase_08 AC 180 ms
62,912 KB
testcase_09 AC 193 ms
62,572 KB
testcase_10 AC 144 ms
60,544 KB
testcase_11 AC 85 ms
54,372 KB
testcase_12 AC 132 ms
58,744 KB
testcase_13 AC 186 ms
62,876 KB
testcase_14 AC 72 ms
53,184 KB
testcase_15 AC 67 ms
53,116 KB
testcase_16 AC 79 ms
54,556 KB
testcase_17 AC 71 ms
53,804 KB
testcase_18 AC 64 ms
53,168 KB
testcase_19 AC 66 ms
53,804 KB
testcase_20 AC 68 ms
53,804 KB
testcase_21 AC 71 ms
54,368 KB
testcase_22 AC 74 ms
54,284 KB
testcase_23 AC 69 ms
54,376 KB
testcase_24 AC 71 ms
54,364 KB
testcase_25 AC 66 ms
53,808 KB
testcase_26 AC 69 ms
54,288 KB
testcase_27 AC 69 ms
54,360 KB
testcase_28 AC 68 ms
54,360 KB
testcase_29 AC 154 ms
57,868 KB
testcase_30 AC 182 ms
62,528 KB
testcase_31 AC 183 ms
62,328 KB
testcase_32 AC 181 ms
61,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*; import java.io.*; import java.math.*;
public class Main{
	//見なくていいよ ここから------------------------------------------
	static class InputIterator{
		ArrayList<String> inputLine = new ArrayList<>(1024);
		int index = 0; int max; String read;
		InputIterator(){
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			try{
				while((read = br.readLine()) != null){
					inputLine.addAll(Arrays.asList(read.split(" ")));
				}
			}catch(IOException e){}
			max = inputLine.size();
		}
		boolean hasNext(){return (index < max);}
		String next(){
			if(hasNext()){
				return inputLine.get(index++);
			}else{
				throw new IndexOutOfBoundsException("There is no more input");
			}
		}
	}
	static HashMap<Integer, String> CONVSTR = new HashMap<>();
	static InputIterator ii = new InputIterator();//This class cannot be used in reactive problem.
	static PrintWriter out = new PrintWriter(System.out);
	static void flush(){out.flush();}
	static void myout(Object t){out.println(t);}
	static void myerr(Object t){System.err.print("debug:");System.err.println(t);}
	static String next(){return ii.next();}
	static boolean hasNext(){return ii.hasNext();}
	static int nextInt(){return Integer.parseInt(next());}
	static long nextLong(){return Long.parseLong(next());}
	static double nextDouble(){return Double.parseDouble(next());}
	static ArrayList<String> nextCharArray(){return myconv(next(), 0);}
	static ArrayList<String> nextStrArray(int size){
		ArrayList<String> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(next());
		}
		return ret;
	}
	static ArrayList<Integer> nextIntArray(int size){
		ArrayList<Integer> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(Integer.parseInt(next()));
		}
		return ret;
	}
	static ArrayList<Long> nextLongArray(int size){
		ArrayList<Long> ret = new ArrayList<>(size);
		for(int i = 0; i < size; i++){
			ret.add(Long.parseLong(next()));
		}
		return ret;
	}
	@SuppressWarnings("unchecked")
	static String myconv(Object list, int no){//only join
		StringBuilder sb = new StringBuilder("");
		String joinString = CONVSTR.get(no);
		if(list instanceof String[]){
			return String.join(joinString, (String[])list);
		}else if(list instanceof long[]){
			long[] tmp = (long[])list;
			if(tmp.length == 0){
				return "";
			}
			sb.append(String.valueOf(tmp[0]));
			for(int i = 1; i < tmp.length; i++){
				sb.append(joinString).append(String.valueOf(tmp[i]));
			}
			return sb.toString();
		}else if(list instanceof int[]){
			int[] tmp = (int[])list;
			if(tmp.length == 0){
				return "";
			}
			sb.append(String.valueOf(tmp[0]));
			for(int i = 1; i < tmp.length; i++){
				sb.append(joinString).append(String.valueOf(tmp[i]));
			}
			return sb.toString();
		}else if(list instanceof ArrayList){
			ArrayList tmp = (ArrayList)list;
			if(tmp.size() == 0){
				return "";
			}
			sb.append(tmp.get(0));
			for(int i = 1; i < tmp.size(); i++){
				sb.append(joinString).append(tmp.get(i));
			}
			return sb.toString();
		}else{
			throw new ClassCastException("Don't join");
		}
	}
	static ArrayList<String> myconv(String str, int no){//only split
		String splitString = CONVSTR.get(no);
		return new ArrayList<String>(Arrays.asList(str.split(splitString)));
	}
	static ArrayList<String> myconv(String str, String no){
		return new ArrayList<String>(Arrays.asList(str.split(no)));
	}
	public static void main(String[] args){
		CONVSTR.put(8, " "); CONVSTR.put(9, "\n"); CONVSTR.put(0, "");
		solve();flush();
	}
	//見なくていいよ ここまで------------------------------------------
	//このコードをコンパイルするときは、「-encoding UTF-8」を指定すること
	static void solve(){//ここがメイン関数
		int N = nextInt();
		int L = nextInt();
		int mod = 1000000007;
		SegTree<Long> sgt = new SegTree<>(N + 1, (x, y) -> (x + y) % mod, 0l);
		sgt.set(0, 1l);
		for(int i = 1; i <= N; i++){
			long sum = sgt.prod(Math.max(0, i - L), i);
			sgt.set(i, sum);
		}
		myout(sgt.get(N));
	}
	//メソッド追加エリア ここから



	//メソッド追加エリア ここまで
}
/**
 * @verified https://atcoder.jp/contests/practice2/tasks/practice2_j
 */
class SegTree<S> {
    final int MAX;

    final int N;
    final java.util.function.BinaryOperator<S> op;
    final S E;

    final S[] data;

    @SuppressWarnings("unchecked")
    public SegTree(int n, java.util.function.BinaryOperator<S> op, S e) {
        this.MAX = n;
        int k = 1;
        while (k < n) k <<= 1;
        this.N = k;
        this.E = e;
        this.op = op;
        this.data = (S[]) new Object[N << 1];
        java.util.Arrays.fill(data, E);
    }

    public SegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e) {
        this(dat.length, op, e);
        build(dat);
    }

    private void build(S[] dat) {
        int l = dat.length;
        System.arraycopy(dat, 0, data, N, l);
        for (int i = N - 1; i > 0; i--) {
            data[i] = op.apply(data[i << 1 | 0], data[i << 1 | 1]);
        }
    }

    public void set(int p, S x) {
        exclusiveRangeCheck(p);
        data[p += N] = x;
        p >>= 1;
        while (p > 0) {
            data[p] = op.apply(data[p << 1 | 0], data[p << 1 | 1]);
            p >>= 1;
        }
    }

    public S get(int p) {
        exclusiveRangeCheck(p);
        return data[p + N];
    }

    public S prod(int l, int r) {
        if (l > r) {
            throw new IllegalArgumentException(
                String.format("Invalid range: [%d, %d)", l, r)
            );
        }
        inclusiveRangeCheck(l);
        inclusiveRangeCheck(r);
        S sumLeft = E;
        S sumRight = E;
        l += N; r += N;
        while (l < r) {
            if ((l & 1) == 1) sumLeft = op.apply(sumLeft, data[l++]);
            if ((r & 1) == 1) sumRight = op.apply(data[--r], sumRight);
            l >>= 1; r >>= 1;
        }
        return op.apply(sumLeft, sumRight);
    }

    public S allProd() {
        return data[1];
    }

    public int maxRight(int l, java.util.function.Predicate<S> f) {
        inclusiveRangeCheck(l);
        if (!f.test(E)) {
            throw new IllegalArgumentException("Identity element must satisfy the condition.");
        }
        if (l == MAX) return MAX;
        l += N;
        S sum = E;
        do {
            l >>= Integer.numberOfTrailingZeros(l);
            if (!f.test(op.apply(sum, data[l]))) {
                while (l < N) {
                    l = l << 1;
                    if (f.test(op.apply(sum, data[l]))) {
                        sum = op.apply(sum, data[l]);
                        l++;
                    }
                }
                return l - N;
            }
            sum = op.apply(sum, data[l]);
            l++;
        } while ((l & -l) != l);
        return MAX;
    }

    public int minLeft(int r, java.util.function.Predicate<S> f) {
        inclusiveRangeCheck(r);
        if (!f.test(E)) {
            throw new IllegalArgumentException("Identity element must satisfy the condition.");
        }
        if (r == 0) return 0;
        r += N;
        S sum = E;
        do {
            r--;
            while (r > 1 && (r & 1) == 1) r >>= 1;
            if (!f.test(op.apply(data[r], sum))) {
                while (r < N) {
                    r = r << 1 | 1;
                    if (f.test(op.apply(data[r], sum))) {
                        sum = op.apply(data[r], sum);
                        r--;
                    }
                }
                return r + 1 - N;
            }
            sum = op.apply(data[r], sum);
        } while ((r & -r) != r);
        return 0;
    }

    private void exclusiveRangeCheck(int p) {
        if (p < 0 || p >= MAX) {
            throw new IndexOutOfBoundsException(
                String.format("Index %d out of bounds for the range [%d, %d).", p, 0, MAX)
            );
        }
    }

    private void inclusiveRangeCheck(int p) {
        if (p < 0 || p > MAX) {
            throw new IndexOutOfBoundsException(
                String.format("Index %d out of bounds for the range [%d, %d].", p, 0, MAX)
            );
        }
    }

    // **************** DEBUG **************** //

    private int indent = 6;

    public void setIndent(int newIndent) {
        this.indent = newIndent;
    }

    @Override
    public String toString() {
        return toSimpleString();
    }

    public String toDetailedString() {
        return toDetailedString(1, 0);
    }

    private String toDetailedString(int k, int sp) {
        if (k >= N) return indent(sp) + data[k];
        String s = "";
        s += toDetailedString(k << 1 | 1, sp + indent);
        s += "\n";
        s += indent(sp) + data[k];
        s += "\n";
        s += toDetailedString(k << 1 | 0, sp + indent);
        return s;
    }

    private static String indent(int n) {
        StringBuilder sb = new StringBuilder();
        while (n --> 0) sb.append(' ');
        return sb.toString();
    }

    public String toSimpleString() {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < N; i++) {
            sb.append(data[i + N]);
            if (i < N - 1) sb.append(',').append(' ');
        }
        sb.append(']');
        return sb.toString();
    }
}
0