Posted by: v1ad | July 28, 2008

Practice week for Round 2

So this week is practice week, because in 4-5 days GCJ Round 2 will take place. The level of problems will be higher and it will be a tough round because out of 2400 competitors only 1000 will remain in game (which is 41%).

So I start this practice week with simulating Round 1 Sub Round 3 of GCJ. After doing the set I realized it was a nice set and slightly easier then SubRound 2 and SubRound 1. I was able to do all 3 problems perfectly in 1:27 hours(if I were in contest this would’ve place me 15th). I liked  all 3 problems. But I think  B was the most crazy one, even thou I coded A + B in half an  hour, and C took me longer.

Let’s talk about B before I finish this post.So after reading it quickly it clicked in my mind that this is DP. If you read the problem you will know what I will be talking about. Quickly I calculated how many DP states are there so I got 2 * 3 * 5 * 7 * length. And for large input length was <= 40. So it was just perfect. Ok here is the recurrence I used. DP[length][two][three][five][seven] = how many are there if I used first length digits, and I have modulo 2 = two, modulo 3 = three, modulo 5 = five, modulo 7 = seven. :) Ok I will end this post leaving my source code here to see it :) (P.S: Wednesday is my 19th Birthday :) )


#include &lt;vector&gt;
#include &lt;list&gt;
#include &lt;map&gt;
#include &lt;set&gt;
#include &lt;deque&gt;
#include &lt;queue&gt;
#include &lt;stack&gt;
#include &lt;bitset&gt;
#include &lt;algorithm&gt;
#include &lt;functional&gt;
#include &lt;numeric&gt;
#include &lt;utility&gt;
#include &lt;sstream&gt;
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;cstdio&gt;
#include &lt;cmath&gt;
#include &lt;cstdlib&gt;
#include &lt;cctype&gt;
#include &lt;string&gt;
#include &lt;cstring&gt;
#include &lt;cstdio&gt;
#include &lt;cmath&gt;
#include &lt;cstdlib&gt;
#include &lt;ctime&gt;

using namespace std;
typedef unsigned long long LL;

void go();

LL DP[44][2][3][5][7];

int main()
{
freopen("file.in", "r", stdin);
freopen("file.out", "w", stdout);
int T;
scanf("%d", &amp;T);
while (T--) go();
//fclose(stdin);
//getchar();
return 0;
}

int Case = 0;
int f(string s, int M) {
int ret = 0;
int i;
for (i = 0; i &lt; s.size(); ++i) ret = (ret * 10 + s[i] - '0') % M;

return ret;
}
void go() {
//clean
int v[4];
int i;
for (i = 0; i &lt; 44; ++i) for (v[0] = 0; v[0] &lt; 2; ++v[0])
for (v[1] = 0; v[1] &lt; 3; ++v[1])
for (v[2] = 0; v[2] &lt; 5; ++v[2])
for (v[3] = 0; v[3] &lt; 7; ++v[3]) DP[i][v[0]][v[1]][v[2]][v[3]] = 0;
DP[0][0][0][0][0] = 1;
string s;
cin &gt;&gt; s;
int n = s.size();

int j, k;
for (i = 1; i &lt;= n; ++i)
for (j = 1; j &lt;= i; ++j) {
//afla module
int m[4];
m[0] = f(s.substr(j-1, i - j + 1), 2);
m[1] = f(s.substr(j-1, i - j + 1), 3);
m[2] = f(s.substr(j-1, i - j + 1), 5);
m[3] = f(s.substr(j-1, i - j + 1), 7);
//+ sau -
for (v[0] = 0; v[0] &lt; 2; ++v[0])
for (v[1] = 0; v[1] &lt; 3; ++v[1])
for (v[2] = 0; v[2] &lt; 5; ++v[2])
for (v[3] = 0; v[3] &lt; 7; ++v[3])       {
//add with m
int sum[4];
for (k = 0; k &lt; 4; ++k) sum[k] = (v[k] + m[k]);
sum[0]%=2; sum[1]%=3; sum[2]%=5; sum[3]%=7;
DP[i][sum[0]][sum[1]][sum[2]][sum[3]] += DP[j-1][v[0]][v[1]][v[2]][v[3]];

for (k = 0; k &lt; 4; ++k) sum[k] = (v[k] - m[k]);
sum[0]+=2000; sum[1]+=3000; sum[2]+=5000; sum[3]+=7000;
sum[0]%=2; sum[1]%=3; sum[2]%=5; sum[3]%=7;
DP[i][sum[0]][sum[1]][sum[2]][sum[3]] += DP[j-1][v[0]][v[1]][v[2]][v[3]];
}
}
LL total = 0;
for (v[0] = 0; v[0] &lt; 2; ++v[0])
for (v[1] = 0; v[1] &lt; 3; ++v[1])
for (v[2] = 0; v[2] &lt; 5; ++v[2])
for (v[3] = 0; v[3] &lt; 7; ++v[3]) if (v[0] * v[1] * v[2] * v[3] == 0) total+=DP[n][v[0]][v[1]][v[2]][v[3]];
printf("Case #%d: ", ++Case);
cout &lt;&lt; total / LL(2) &lt;&lt; '\n';

}

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.