提出 #9587479
ソースコード 拡げる
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template<class S, class T> inline S min_L(S a,T b){
return a<=b?a:b;
}
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
sort(a, a+N);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
int i;
pair<T1, T2> *arr;
walloc1d(&arr, N, &mem);
for(i=(0);i<(N);i++){
arr[i].first = a[i];
arr[i].second = b[i];
}
sort(arr, arr+N);
for(i=(0);i<(N);i++){
a[i] = arr[i].first;
b[i] = arr[i].second;
}
}
inline void rd(long long &x){
int k;
int m=0;
x=0;
for(;;){
k = getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline void wt_L(char a){
putchar_unlocked(a);
}
inline void wt_L(int x){
int s=0;
int m=0;
char f[10];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
putchar_unlocked('-');
}
while(s--){
putchar_unlocked(f[s]+'0');
}
}
inline void wt_L(long long x){
int s=0;
int m=0;
char f[20];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
putchar_unlocked('-');
}
while(s--){
putchar_unlocked(f[s]+'0');
}
}
template<class T, class U> inline T GCD_L(T a, U b){
T r;
while(b){
r=a;
a=b;
b=r%a;
}
return a;
}
template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
a[k] = aval;
}
template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
a[k] = aval;
b[k] = bval;
}
template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
}
template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){
int i;
sz++;
for(i=sz-1;i>k;i--){
a[i] = a[i-1];
}
for(i=sz-1;i>k;i--){
b[i] = b[i-1];
}
for(i=sz-1;i>k;i--){
c[i] = c[i-1];
}
for(i=sz-1;i>k;i--){
d[i] = d[i-1];
}
a[k] = aval;
b[k] = bval;
c[k] = cval;
d[k] = dval;
}
template<class S, class T> inline S chmax(S &a, T b){
if(a<b){
a=b;
}
return a;
}
long long N;
long long X;
long long D;
int sz;
long long mns[200000];
long long mxs[200000];
int main(){
wmem = memarr;
long long i;
long long j;
long long g;
long long mn;
long long mx;
long long all;
long long res = 0;
rd(N);
rd(X);
rd(D);
if(X==D && D==0){
wt_L(1);
wt_L('\n');
return 0;
}
if(D==0){
wt_L(N+1);
wt_L('\n');
return 0;
}
if(D < 0){
D = -D;
X = -X;
}
g =GCD_L(abs(X), D);
X /= g;
D /= g;
all = N * (N-1) / 2 * D;
int Q5VJL1cS = min_L(D, N+1);
for(i=(0);i<(Q5VJL1cS);i++){
sz = 0;
for(j=(i);j<(N+1);j+=(D)){
mx = X * (N-j) + (all - j * (j-1) / 2 * D);
mn = X * (N-j) + ((N-j) * (N-j-1) / 2 * D);
arrInsert(sz,sz,mns,mn,mxs,mx);
}
sortA_L(sz, mns, mxs);
mx = -4611686016279904256LL;
for(j=(0);j<(sz);j++){
chmax(mns[j], mx + D);
chmax(mx, mxs[j]);
if(mxs[j] >= mns[j]){
res += (mxs[j] - mns[j]) / D + 1;
}
}
}
wt_L(res);
wt_L('\n');
return 0;
}
// cLay varsion 20200119-1
// --- original code ---
// ll N, X, D;
//
// int sz;
// ll mns[2d5], mxs[2d5];
//
// {
// ll i, j, g, mn, mx, all;
// ll res = 0;
//
// rd(N,X,D);
//
// if(X==D==0) wt(1), return 0;
// if(D==0) wt(N+1), return 0;
//
// if(D < 0) D = -D, X = -X;
// g = gcd(abs(X),D);
// X /= g;
// D /= g;
//
// all = N * (N-1) / 2 * D;
// REP(i,min(D,N+1)){
// sz = 0;
// rep(j,i,N+1,D){
// mx = X * (N-j) + (all - j * (j-1) / 2 * D);
// mn = X * (N-j) + ((N-j) * (N-j-1) / 2 * D);
// arrInsert(sz,sz,mns,mn,mxs,mx);
// }
//
// sortA(sz, mns, mxs);
// mx = -ll_inf;
// rep(j,sz){
// mns[j] >?= mx + D;
// mx >?= mxs[j];
// if(mxs[j] >= mns[j]) res += (mxs[j] - mns[j]) / D + 1;
// }
// }
//
// wt(res);
// }
提出情報
| 提出日時 |
|
| 問題 |
F - Sum Difference |
| ユーザ |
LayCurse |
| 言語 |
C++14 (GCC 5.4.1) |
| 得点 |
600 |
| コード長 |
5306 Byte |
| 結果 |
AC |
| 実行時間 |
10 ms |
| メモリ |
8448 KiB |
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
600 / 600 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
s1.txt, s2.txt, s3.txt |
| All |
01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, s1.txt, s2.txt, s3.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 01.txt |
AC |
2 ms |
2304 KiB |
| 02.txt |
AC |
2 ms |
2304 KiB |
| 03.txt |
AC |
2 ms |
2304 KiB |
| 04.txt |
AC |
2 ms |
2304 KiB |
| 05.txt |
AC |
2 ms |
2304 KiB |
| 06.txt |
AC |
2 ms |
2304 KiB |
| 07.txt |
AC |
1 ms |
2304 KiB |
| 08.txt |
AC |
2 ms |
2304 KiB |
| 09.txt |
AC |
2 ms |
2304 KiB |
| 10.txt |
AC |
2 ms |
2304 KiB |
| 11.txt |
AC |
1 ms |
2304 KiB |
| 12.txt |
AC |
2 ms |
2304 KiB |
| 13.txt |
AC |
1 ms |
2304 KiB |
| 14.txt |
AC |
1 ms |
2304 KiB |
| 15.txt |
AC |
2 ms |
2304 KiB |
| 16.txt |
AC |
1 ms |
2304 KiB |
| 17.txt |
AC |
1 ms |
2304 KiB |
| 18.txt |
AC |
1 ms |
2304 KiB |
| 19.txt |
AC |
1 ms |
2304 KiB |
| 20.txt |
AC |
1 ms |
2304 KiB |
| 21.txt |
AC |
2 ms |
2304 KiB |
| 22.txt |
AC |
2 ms |
2304 KiB |
| 23.txt |
AC |
5 ms |
2304 KiB |
| 24.txt |
AC |
6 ms |
2304 KiB |
| 25.txt |
AC |
6 ms |
2304 KiB |
| 26.txt |
AC |
6 ms |
2304 KiB |
| 27.txt |
AC |
6 ms |
2304 KiB |
| 28.txt |
AC |
6 ms |
2304 KiB |
| 29.txt |
AC |
6 ms |
2304 KiB |
| 30.txt |
AC |
6 ms |
2304 KiB |
| 31.txt |
AC |
5 ms |
2304 KiB |
| 32.txt |
AC |
6 ms |
2304 KiB |
| 33.txt |
AC |
6 ms |
2304 KiB |
| 34.txt |
AC |
6 ms |
2304 KiB |
| 35.txt |
AC |
7 ms |
2432 KiB |
| 36.txt |
AC |
9 ms |
8448 KiB |
| 37.txt |
AC |
7 ms |
2816 KiB |
| 38.txt |
AC |
7 ms |
3072 KiB |
| 39.txt |
AC |
8 ms |
2432 KiB |
| 40.txt |
AC |
10 ms |
8448 KiB |
| 41.txt |
AC |
8 ms |
2944 KiB |
| 42.txt |
AC |
7 ms |
2432 KiB |
| 43.txt |
AC |
8 ms |
2816 KiB |
| 44.txt |
AC |
9 ms |
8448 KiB |
| 45.txt |
AC |
7 ms |
2560 KiB |
| 46.txt |
AC |
6 ms |
2304 KiB |
| 47.txt |
AC |
6 ms |
2304 KiB |
| 48.txt |
AC |
2 ms |
2304 KiB |
| s1.txt |
AC |
2 ms |
2304 KiB |
| s2.txt |
AC |
1 ms |
2304 KiB |
| s3.txt |
AC |
1 ms |
2304 KiB |