結果

問題 No.752 mod数列
ユーザー beetbeet
提出日時 2018-11-09 22:21:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,435 ms / 2,000 ms
コード長 1,341 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 209,456 KB
実行使用メモリ 393,964 KB
最終ジャッジ日時 2024-05-01 05:00:09
合計ジャッジ時間 29,425 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 662 ms
393,848 KB
testcase_01 AC 611 ms
393,964 KB
testcase_02 AC 608 ms
393,764 KB
testcase_03 AC 606 ms
393,916 KB
testcase_04 AC 606 ms
393,868 KB
testcase_05 AC 599 ms
393,840 KB
testcase_06 AC 597 ms
393,900 KB
testcase_07 AC 602 ms
393,672 KB
testcase_08 AC 610 ms
393,820 KB
testcase_09 AC 603 ms
393,852 KB
testcase_10 AC 612 ms
393,868 KB
testcase_11 AC 606 ms
393,704 KB
testcase_12 AC 605 ms
393,852 KB
testcase_13 AC 613 ms
393,736 KB
testcase_14 AC 607 ms
393,848 KB
testcase_15 AC 627 ms
393,844 KB
testcase_16 AC 628 ms
393,884 KB
testcase_17 AC 1,435 ms
393,844 KB
testcase_18 AC 631 ms
393,676 KB
testcase_19 AC 636 ms
393,860 KB
testcase_20 AC 644 ms
393,864 KB
testcase_21 AC 649 ms
393,676 KB
testcase_22 AC 650 ms
393,768 KB
testcase_23 AC 646 ms
393,864 KB
testcase_24 AC 642 ms
393,812 KB
testcase_25 AC 1,004 ms
393,832 KB
testcase_26 AC 1,220 ms
393,816 KB
testcase_27 AC 912 ms
393,860 KB
testcase_28 AC 1,105 ms
393,704 KB
testcase_29 AC 860 ms
393,832 KB
testcase_30 AC 1,395 ms
393,892 KB
testcase_31 AC 607 ms
393,824 KB
testcase_32 AC 603 ms
393,832 KB
testcase_33 AC 605 ms
393,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  Int p,q;
  cin>>p>>q;
  
  const Int MAX = 5e7;
  vector<Int> dp(MAX,0);
  for(Int i=1;i<MAX;i++) dp[i]=dp[i-1]+(p%i);

  map<Int, Int> dp2;
  dp2[MAX-1]=dp[MAX-1];
  auto calc=
    [&](Int x)->Int{
      if(x<MAX) return dp[x];
      /*
        if(dp2.count(x)) return dp2[x];
        Int &res=dp2[x];
        Int k=(--dp2.upper_bound(x))->second+1;      
        res=dp2[k-1]+(x-k+1)*p;
      */      
      Int k=MAX,res=dp[k-1]+(x-k+1)*p;
      
      auto sub=
        [&](Int a,Int b){
          assert(p/a==p/b);
          res-=(p/a)*(a+b)*(b-a+1)/2;          
        };
      
      while(k<x){
        if(p/k==p/x){
          sub(k,x);
          break;
        }
        Int l=k,r=x;
        while(l+1<r){
          Int m=(l+r)>>1;
          if(p/k==p/m) l=m;
          else r=m;
        }
        sub(k,l);
        k=r;
      }
      return res;
    };
  
  for(Int i=0;i<q;i++){
    Int l,r;
    cin>>l>>r;
    cout<<calc(r)-calc(l-1)<<"\n";
  }
  cout<<flush;
  return 0;
}
0