結果

問題 No.752 mod数列
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2018-09-01 01:35:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 240 ms / 2,000 ms
コード長 2,360 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 100,672 KB
実行使用メモリ 9,992 KB
最終ジャッジ日時 2023-10-12 00:17:00
合計ジャッジ時間 8,947 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,600 KB
testcase_01 AC 2 ms
7,616 KB
testcase_02 AC 2 ms
7,588 KB
testcase_03 AC 3 ms
7,540 KB
testcase_04 AC 2 ms
7,580 KB
testcase_05 AC 3 ms
7,572 KB
testcase_06 AC 2 ms
7,556 KB
testcase_07 AC 2 ms
7,880 KB
testcase_08 AC 3 ms
7,564 KB
testcase_09 AC 3 ms
7,548 KB
testcase_10 AC 8 ms
9,396 KB
testcase_11 AC 6 ms
8,716 KB
testcase_12 AC 8 ms
9,148 KB
testcase_13 AC 7 ms
8,796 KB
testcase_14 AC 9 ms
9,428 KB
testcase_15 AC 192 ms
7,520 KB
testcase_16 AC 234 ms
9,740 KB
testcase_17 AC 240 ms
9,896 KB
testcase_18 AC 193 ms
7,536 KB
testcase_19 AC 208 ms
7,740 KB
testcase_20 AC 200 ms
7,764 KB
testcase_21 AC 202 ms
7,696 KB
testcase_22 AC 204 ms
7,644 KB
testcase_23 AC 208 ms
7,764 KB
testcase_24 AC 204 ms
7,656 KB
testcase_25 AC 67 ms
9,992 KB
testcase_26 AC 176 ms
9,660 KB
testcase_27 AC 127 ms
9,380 KB
testcase_28 AC 164 ms
9,648 KB
testcase_29 AC 212 ms
8,976 KB
testcase_30 AC 208 ms
9,124 KB
testcase_31 AC 3 ms
7,468 KB
testcase_32 AC 2 ms
7,508 KB
testcase_33 AC 6 ms
9,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
using namespace std;

typedef __int128 ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=(b-1);i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007

ostream &operator<<(ostream &os, __int128_t value) {
  if (ostream::sentry(os)) {
    __uint128_t tmp = value < 0 ? -value : value;
    char buffer[64];
    char *d = end(buffer);
    do {
      --d;
      *d = "0123456789"[tmp % 10];
      tmp /= 10;
    } while (tmp != 0);
    if (value < 0) {
      --d;
      *d = '-';
    }
    int len = end(buffer) - d;
    if (os.rdbuf()->sputn(d, len) != len) {
      os.setstate(ios_base::badbit);
    }
  }
  return os;
}

istream &operator>>(istream &is, __int128_t &value) {
  string in;
  is >> in;
  value = 0;
  for (const char &c : in) {
    if ('0' <= c && c <= '9') value = 10 * value + (c - '0');
  }
  if (in[0] == '-') value *= -1;
  return is;
}

ll p;
ll n;

ll d1[100000];
ll d2[100000];
ll d3[100000];
ll d4[100000];
ll d5[100000];

ll f1(ll a, ll d, ll n){
	return a*n+n*(n-1)*d/2;
}

ll f(ll t){
	if(p<=t){
		return d5[n-1]+p*(t-p);
	}
	long long low = 0;
	long long high = n-1;
	
	while(low < high){
	  long long mid = (high + low + 1) >> 1;
	
	  if(d1[mid] <= t){
	    low = mid;
	  }
	  else{
	    high = mid - 1;
	  }
	}
	ll ret = 0;
	if(low>0)ret = d5[low-1];
	ret += f1(d3[low],d2[low],(t-d1[low])+1);
	return ret;
}

int main(void) {
	ll q;
	cin>>p>>q;
	ll i = 0;
	ll index = 1;
	while(1){
		ll a = p/index;
		if(a==0)break;
		ll b = p-a*index;
		ll c = b/a+1;
		d1[i] = index;
		d2[i] = -a;
		d3[i] = b;
		d4[i] = c;
		i++;
		index+=c;
	}
	n = i;
	rep(i,0,n){
		d5[i] += f1(d3[i],d2[i],d4[i]);
	}
	rep(i,1,n){
		d5[i] += d5[i-1];
	}
	rep(i,0,q){
		ll a,b;
		cin>>a>>b;
		cout << f(b)-f(a-1) << endl;
	}
	return 0;
}
0