結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー ku_material_ro
提出日時 2016-10-22 03:29:21
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 2,999 bytes
コンパイル時間 643 ms
コンパイル使用メモリ 64,380 KB
実行使用メモリ 15,152 KB
最終ジャッジ日時 2024-11-23 17:03:52
合計ジャッジ時間 142,879 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 55 WA * 1 TLE * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
using namespace std;

int kaijo(long a){
	long n=1;
	if (a > 0){
		for (long i = a; i > 0; i--){
			n *= i;
			n = n % 1000000007;
		}
	}
	else{
		n = 1;
	}
	return n;
}

int permtation(long a,long b){
	long n = 1;
	if (b != 0){
		for (long i = 0; i < b; i++){
			n *= (a-i);
			n = n % 1000000007;
		}
	}
	else{
		n = 1;
	}
	return n;
}

int main(){
	long m;
	long p1;
	long a = 0;
	long sho;
	long amari;
	long cnt2=0;
	string s;
	string s1;
	long h;
	long cnt = 0;
	long ans;
	long p, k;
	int flag=0;
	cin >> m;
	cin.ignore();
	getline(cin, s);
	if (s == "0") flag = 1;
	while (1){
		s1 = s;
		//cout << s1 << endl;
		a = s1.find(" ", 0);
		if (a == -1){
			h = stoi(s1);
			m -= h;
			cnt++;
			break;
		}
		else{
			s1.erase(a,s1.length());
			h = stoi(s1);
			m -= h;
			cnt++;
			s.erase(0, a + 1);
			//cout << s << endl;
		}
	}
	long judge = m - cnt + 1;
	//cout << m << endl;
	//cout << cnt << endl;
	//cout << judge << endl;
	if (flag == 1){
		cout << "1" << endl;
	}else if (judge <0){
		cout << "NA" << endl;
	}
	else{
		if (judge>cnt){
			p = permtation(m + 1, cnt);
			k = kaijo(cnt);
			
			while (1){
				ans = p / k;
				if (ans*k == p){
					cout << ans << endl;
					break;
				}
				else{
					if (p <k){
						p1 = p;
						while (1){
							sho = (k - p1) / (1000000007 / k);
							amari = (k - p1) %(1000000007 / k);
							if (amari == 0){
								cnt2 += sho;
								break;
							}
							else{
								cnt2 += sho + 1;
								p1 = (p1 + (sho + 1) * 1000000007 / k) - k;
							}
						}
						p += 1000000007 * cnt2;
					}
					if (p >k){
						p1 = p-k;
						while (1){
							sho = (k - p1) / (1000000007 / k);
							amari = (k - p1) % (1000000007 / k);
							if (amari == 0){
								cnt2 += sho;
								break;
							}
							else{
								cnt2 += sho + 1;
								p1 = (p1 + (sho + 1) * 1000000007 / k) - k;
							}
						}
						p += 1000000007 * cnt2;
					}
				}
			}
		}
		else{
			p = permtation(m + 1, judge);
			k = kaijo(judge);
			
			while (1){
				ans = p / k;
				if (ans*k == p){
					cout << ans << endl;
					break;
				}
				else{
					if (p <k){
						p1 = p;
						while (1){
							sho = (k - p1) / (1000000007 / k);
							amari = (k - p1) % (1000000007 / k);
							if (amari == 0){
								cnt2 += sho;
								break;
							}
							else{
								cnt2 += sho + 1;
								p1 = (p1 + (sho + 1) * 1000000007 / k) - k;
							}
						}
						p += 1000000007 * cnt2;
					}
					if (p >k){
						p1 = p - k;
						while (1){
							sho = (k - p1) / (1000000007 / k);
							amari = (k - p1) % (1000000007 / k);
							if (amari == 0){
								cnt2 += sho;
								break;
							}
							else{
								cnt2 += sho + 1;
								p1 = (p1 + (sho + 1) * 1000000007 / k) - k;
							}
						}
						p += 1000000007 * cnt2;
					}
				}
			}
		}
		//ans = kaijo(m + 1) / (kaijo(m - cnt + 1)*kaijo(cnt));
		//cout << ans << endl;
		//cout << m << endl;
	}
	return 0;
}
0