結果

問題 No.752 mod数列
ユーザー WA_TLEWA_TLE
提出日時 2018-11-09 21:50:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 2,443 bytes
コンパイル時間 1,525 ms
コンパイル使用メモリ 141,240 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 04:45:37
合計ジャッジ時間 5,793 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 4 ms
6,940 KB
testcase_12 AC 5 ms
6,944 KB
testcase_13 AC 5 ms
6,940 KB
testcase_14 AC 5 ms
6,940 KB
testcase_15 AC 156 ms
6,944 KB
testcase_16 AC 182 ms
6,940 KB
testcase_17 AC 178 ms
6,944 KB
testcase_18 AC 158 ms
6,940 KB
testcase_19 AC 165 ms
6,944 KB
testcase_20 AC 159 ms
6,940 KB
testcase_21 AC 164 ms
6,940 KB
testcase_22 AC 160 ms
6,944 KB
testcase_23 AC 156 ms
6,940 KB
testcase_24 AC 161 ms
6,944 KB
testcase_25 AC 51 ms
6,940 KB
testcase_26 AC 134 ms
6,944 KB
testcase_27 AC 95 ms
6,944 KB
testcase_28 AC 122 ms
6,940 KB
testcase_29 AC 164 ms
6,944 KB
testcase_30 AC 163 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
using namespace std;
typedef long long int llint;
typedef double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<fixed<<setprecision(20);
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod=1000000007;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	llint P,Q,i;cin>>P>>Q;
	//商の区間で区切る
	vector<pair<llint,llint>>wa;
	//場所、それまでの合計
	wa.reserve(sqrt(P)*2 +10);
	llint mae=mod;
	for(i=1;;i++){
		if(P/i==mae){break;}
		mae=P/i;
		wa.pub(mp(mae+1,0));
	}
	for(i=mae;i>=1;i--){wa.pub(mp(i,0));}
	REV(wa);
	llint gen=0;
	for(i=1;i<wa.size();i++){
		llint sa=wa[i].fir-wa[i-1].fir;
		gen+=sa*P - (P/wa[i-1].fir)*(wa[i].fir+wa[i-1].fir-1)*sa/2;
		wa[i].sec=gen;
	}
	for(i=0;i<Q;i++){
		llint L,R;cin>>L>>R;R++;
		llint ans=0;
		int Rbas=LBI(wa,mp(R,big))-1;
		//cerr<<"de"<<wa[Rbas].fir<<endl;
		ans+=wa[Rbas].sec;
		llint saR=R-wa[Rbas].fir;
		ans+=saR*P - (P/wa[Rbas].fir)*(R+wa[Rbas].fir-1)*saR/2;
		int Lbas=LBI(wa,mp(L,big))-1;
		ans-=wa[Lbas].sec;
		llint saL=L-wa[Lbas].fir;
		ans-=saL*P - (P/wa[Lbas].fir)*(L+wa[Lbas].fir-1)*saL/2;
		cout<<ans<<endl;
	}
	return 0;
}
0