我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2015年4月17日 星期五

[UVa] 10176 - Ocean Deep! - Make it shallow!!

題目網址: http://goo.gl/LJTXH6

題意:
(from luckycat)


解法: 跟在 10 進位底下如何算一個大數 N 是否為 11 的倍數有點像。

TAG: Math

注意: input 的處理要以字串字串讀,不能以字元字元讀,吃了兩次 TLE...。

程式碼:
/**
* Tittle: 10176 - Ocean Deep! - Make it shallow!!
* Author: Cheng-Shih, Wong
* Date: 2015/04/17
*/
// include files
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
// definitions
#define FOR(i,a,b) for( int i=(a),_n=(b); i<=_n; ++i )
#define clr(x,v) memset( x, v, sizeof(x) )
typedef long long ll;
// declarations
// functions
// main function
int main( void )
{
ll box[20];
ll rmd;
int ss;
string str;
char buf[105];
// input
while( scanf( "%s", buf )==1 ) {
str = buf;
// solve
while( str[str.size()-1] != '#' ) {
scanf( "%s", buf );
str += buf;
}
str.pop_back();
clr( box, 0 );
ss = str.size()-1;
FOR( i, 0, ss ) {
if( str[i]=='1' )
++box[(ss-i)%17];
}
FOR( i, 0, 16 )
box[i] %= 131071;
rmd = 0;
FOR( i, 0, 16 )
rmd = (rmd+box[i]*(1LL<<i))%131071;
// output
if( rmd ) puts("NO");
else puts("YES");
}
return 0;
}

沒有留言:

張貼留言

任何意見都樂意傾聽