結果

問題 No.752 mod数列
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-31 12:40:48
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 501 ms / 2,000 ms
コード長 2,247 bytes
コンパイル時間 661 ms
コンパイル使用メモリ 108,656 KB
実行使用メモリ 94,068 KB
最終ジャッジ日時 2023-09-03 23:35:49
合計ジャッジ時間 13,707 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
81,420 KB
testcase_01 AC 134 ms
81,932 KB
testcase_02 AC 134 ms
81,672 KB
testcase_03 AC 134 ms
81,380 KB
testcase_04 AC 134 ms
81,156 KB
testcase_05 AC 134 ms
81,676 KB
testcase_06 AC 134 ms
81,616 KB
testcase_07 AC 134 ms
81,064 KB
testcase_08 AC 133 ms
82,716 KB
testcase_09 AC 134 ms
81,632 KB
testcase_10 AC 138 ms
81,832 KB
testcase_11 AC 138 ms
82,760 KB
testcase_12 AC 137 ms
82,152 KB
testcase_13 AC 137 ms
82,392 KB
testcase_14 AC 137 ms
81,308 KB
testcase_15 AC 455 ms
86,264 KB
testcase_16 AC 462 ms
85,216 KB
testcase_17 AC 501 ms
92,060 KB
testcase_18 AC 466 ms
85,464 KB
testcase_19 AC 465 ms
85,512 KB
testcase_20 AC 485 ms
92,664 KB
testcase_21 AC 482 ms
92,180 KB
testcase_22 AC 484 ms
91,812 KB
testcase_23 AC 482 ms
86,472 KB
testcase_24 AC 481 ms
85,332 KB
testcase_25 AC 231 ms
83,752 KB
testcase_26 AC 413 ms
84,244 KB
testcase_27 AC 331 ms
84,100 KB
testcase_28 AC 392 ms
89,768 KB
testcase_29 AC 473 ms
84,536 KB
testcase_30 AC 481 ms
94,068 KB
testcase_31 AC 134 ms
81,572 KB
testcase_32 AC 134 ms
81,420 KB
testcase_33 AC 133 ms
81,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


enum B = 10^^7;
enum C = 10^^2;

long P;
int Q;
long[] L, R;

void main() {
  try {
    for (; ; ) {
      P = readLong();
      Q = readInt();
      L = new long[Q];
      R = new long[Q];
      foreach (q; 0 .. Q) {
        L[q] = readLong();
        R[q] = readLong();
      }
      
      auto sum = new long[B + 2];
      sum[1] = 0;
      foreach (n; 1 .. B + 1) {
        sum[n + 1] = sum[n] + P % n;
      }
      
      foreach (q; 0 .. Q) {
        long ans;
        
        // 1 <= n <= B
        {
          const a = cast(int)(max(L[q], 1));
          const b = cast(int)(min(R[q], B));
          if (a <= b) {
            ans += (sum[b + 1] - sum[a]);
          }
        }
        
        // floor(P / n) = c
        //   c n <= P < (c + 1) n
        foreach (c; 0 .. C + 1) {
          const a = max(P / (c + 1) + 1, L[q], B + 1);
          const b = (c == 0) ? R[q] : min(P / c, R[q]);
          if (a <= b) {
            ans += P * (b - a + 1);
            ans -= c * ((a + b) * (b - a + 1) / 2);
          }
        }
        
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0