結果

問題 No.752 mod数列
ユーザー WA_TLEWA_TLE
提出日時 2018-11-09 21:50:21
言語 C++17
(gcc 12.2.0 + boost 1.81.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 2,443 bytes
コンパイル時間 1,232 ms
実行使用メモリ 4,152 KB
最終ジャッジ日時 2022-12-26 19:56:05
合計ジャッジ時間 6,213 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
3,600 KB
testcase_01 AC 1 ms
3,424 KB
testcase_02 AC 2 ms
3,544 KB
testcase_03 AC 1 ms
3,512 KB
testcase_04 AC 2 ms
3,560 KB
testcase_05 AC 2 ms
3,604 KB
testcase_06 AC 1 ms
3,596 KB
testcase_07 AC 1 ms
3,600 KB
testcase_08 AC 1 ms
3,424 KB
testcase_09 AC 2 ms
3,520 KB
testcase_10 AC 4 ms
3,960 KB
testcase_11 AC 4 ms
3,544 KB
testcase_12 AC 5 ms
3,636 KB
testcase_13 AC 5 ms
3,660 KB
testcase_14 AC 5 ms
3,880 KB
testcase_15 AC 158 ms
3,456 KB
testcase_16 AC 176 ms
3,780 KB
testcase_17 AC 171 ms
4,152 KB
testcase_18 AC 158 ms
3,516 KB
testcase_19 AC 162 ms
3,544 KB
testcase_20 AC 157 ms
3,572 KB
testcase_21 AC 163 ms
3,628 KB
testcase_22 AC 157 ms
3,544 KB
testcase_23 AC 157 ms
3,592 KB
testcase_24 AC 155 ms
3,556 KB
testcase_25 AC 48 ms
3,780 KB
testcase_26 AC 132 ms
3,928 KB
testcase_27 AC 94 ms
3,776 KB
testcase_28 AC 123 ms
3,772 KB
testcase_29 AC 157 ms
3,612 KB
testcase_30 AC 162 ms
3,660 KB
testcase_31 AC 1 ms
3,400 KB
testcase_32 AC 2 ms
3,404 KB
testcase_33 AC 3 ms
3,508 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