結果

問題 No.278 連続する整数の和(2)
ユーザー fiordfiord
提出日時 2015-09-08 12:50:53
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,288 bytes
コンパイル時間 1,372 ms
コンパイル使用メモリ 151,556 KB
実行使用メモリ 4,512 KB
最終ジャッジ日時 2023-08-09 06:41:36
合計ジャッジ時間 2,289 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 11 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,512 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 9 ms
4,380 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
ull calc(ull a,ull b){
	ull ans=1;
	while(b>0){
		if(b%2)	ans*=a;
		a*=a;
		b/=2;
	}
	return ans;
}
	
int main(){
	ull n;	cin>>n;
	if(n<3){
		cout<<1<<endl;
		return 0;
	}
	/*n以上の素数xを使ってn個の連続する自然数の和は
	 * x(n+n*(n-1)/2)と表せる。よってnとn*(n-1)/2の最大公約数をgとおき、
	 * g = a^s * b^t *c^u	と素因数分解できたとすると
	 * Σx=(a^0+a^1...+a^s)*(b^0+b^1...+b^t)*(c^0+c^1...+c^u)
	 * =(a^(s+1)-1)/a-1*。
	 * ここで、n*(n-1)/2は64bit型に収まらないので、場合分け。
	 * 1.(n-1)が偶数のとき	nで括ってxn(1+(n-1)/2)。g=n。
	 * 2.nが偶数のとき	n/2で括ってxn/2(2+(n-1))。n-1は奇数だからg=n/2。
	 * あとは適当に因数分解の結果をmapなどに放っておけば解ける?
	 * (答えが64bitに収まるかは知らない)*/
	ull g,cp;
	if(n%2==0)	g=n/2;
	else 	g=n;
	cp=g;
	map<ull,ull> ap;
	for(ull i=2;i*i<=g;i++){
		while(g%i==0)	ap[i]++,g/=i;
	}
	if(ap.size()==0){
		cout<<cp+1<<endl;
		return 0;
	}
	if(g!=1)	ap[g]++;
	ull ans=1;
	for(auto it=ap.begin();it!=ap.end();it++){
		ull a=it->first,s=it->second;
		ans*=(calc(a,s+1)-1)/(a-1);
	}
	cout<<ans<<endl;
	return 0;
}
0