結果

問題 No.752 mod数列
ユーザー WA_TLE
提出日時 2018-11-09 21:50:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 2,443 bytes
コンパイル時間 1,421 ms
コンパイル使用メモリ 136,528 KB
最終ジャッジ日時 2025-01-06 15:59:49
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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