結果

問題 No.8015 アンチローリングハッシュ
ユーザー 西尾西尾
提出日時 2018-03-07 13:06:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 1,690 ms
コンパイル使用メモリ 174,668 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-01 20:38:46
合計ジャッジ時間 2,903 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

unsigned long long HASH
(
	const string S,
	const unsigned long long a,
	const unsigned long long b
)
{
int i;
int iSize;
unsigned long long h;

	h = 0;
	iSize = S.size();
	for( i = 0; i < iSize; i++ )
	{
		h = ( h * a + S[ i ] ) % b;
	}

	return h;
}

bool NextStr( string &S )
{
int i;
int iSize;

	i = 0;
	iSize = S.size();
	while( i < iSize )
	{
		S[ i ]++;
		if( S[ i ] > 'z' )
		{
			S[ i ] = 'a';
			i++;
		}
		else
			return true;
	}

	return false;
}
	
bool CheckHash
(
	const int a,
	const int b,
	const int iDigit
)
{
string S( iDigit, 'a' );
unsigned long long h;
map<unsigned long long,string> m;
map<unsigned long long,string>::iterator it;

	while( true )
	{
		h = HASH( S, a, b );
		it = m.find( h );
		if( it != m.end() )
		{
			cout << m[ h ] << endl;
			cout << S << endl;

			return true;
		}
		else
			m[ h ] = S;

		if( ! NextStr( S ) ) return false;
	}

	return false;
}

bool CheckHash( const int a, const int b )
{
int iDigit;

	for( iDigit = 2; iDigit < 10; iDigit++ )
	{
		if( CheckHash( a, b, iDigit ) )
			return true;
	}

	return false;
}

int main()
{
int a;
int b;

	cin >> a;
	cin >> b;
	CheckHash( a, b );

	return 0;
}
0