Ultraviolet A 11125 Arrange Some Marbles

Source: Internet
Author: User

UVA_11125

At first, I was scared by the 3000 sets of data in this question. However, after careful analysis, I found that the complexity is not mainly determined by the 3000 sets of data. Therefore, the 3000 sets are only for the publisher.

After seeing the question, we found that putting stones can be viewed as a stage and a stage. Each time we put a group, there are only some restrictions, can we perform dp counting in stages?

With this question, we continue to analyze the question and find that there is no clear sequence in the process of taking stones. Therefore, we cannot manually define a sequence for taking stones, therefore, the process of getting a group of stones can only be implemented by enumeration. In order to enumerate the stones, we must know how many stones are left in each class at each stage. In this way, we may wish to regard the number of stones in each class as a State, however, this status seems to be troublesome, but it doesn't matter. If you think about it carefully, you will find that the number of stones is relatively small. We can compress this status into an octal number, in this way, there can be a maximum of 8*8*8*8 states, few.

The status has been initially completed, but we found that there is a problem not solved. How can we avoid the same color or quantity as the previous group? Let's further refine the status. Add two integers, pc and pn, and record the color of the stones in front and the number of stones in front. But what about the first group of stones? That is to say, what should we initialize the pc and pn at the beginning? Wait, it seems that there is another problem we haven't solved, and the question still requires the same number of colors at the beginning and end.

But it still doesn't matter. Let's solve these two problems together. It is really not easy to handle the difference at the beginning and end, but we will be lazy if it is not easy to handle. How can we be lazy? Enumeration. If the status of the Group at the beginning or end is specified, the problem is solved. We may want to specify the tail state, color as fc, number as fn, and compress the two integers into the dp state. In this way, we naturally know that the pc and pn should be initialized to fc and fn at the beginning, so as long as the subsequent process is not in the same color as the pc, and not in the same number as the pn, naturally, it ensures that the beginning and end are of different colors and quantities.

Now there seems to be no too many problems. The rest is to see if the complexity can afford it. If not, we have to find a way to optimize the dp. The total status is 4*3*4*3*8*8*8, and each status can be transferred 4*3 times at most. The total status is 7077888, and the time is correct.

Oh, yes, there are still 3000 sets of data. In fact, the complexity we just calculated includes all the States, as long as we keep the state of memory-based search, the number of data groups does not matter. We will not calculate the status again, but increase the number of queries.

To save some space, we use 4-in for the first four states, and 8-in for the last four States to compress the eight numbers into an integer.

#include<stdio.h>
#include<string.h>
#define MAXD 1100000
int f[MAXD], a[5], b[5];
int dp(int state)
{
int i, j, k, t, st = state, ans = 0, fc, fn, pc, pn;
if(f[state] != -1)
return f[state];
fc = st % 4, st /= 4;
fn = st % 4, st /= 4;
pc = st % 4, st /= 4;
pn = st % 4, st /= 4;
k = 0;
for(i = 0; i < 4; i ++)
{
b[i] = st % 8, st /= 8;
k += b[i];
}
if(k == 0)
{
if(pc == fc && pn == fn)
return f[state] = 1;
else
return f[state] = 0;
}
for(i = 0; i < 4; i ++)
{
if(i == pc)
continue;
for(j = 1; j <= 3 && j <= b[i]; j ++)
{
if(j == pn)
continue;
b[i] -= j;
for(k = 3, t = 0; k >= 0; k --)
t = t * 8 + b[k];
t = (((t * 4 + j) * 4 + i) * 4 + fn) * 4 + fc;
ans += dp(t);
b[i] += j;
}
}
return f[state] = ans;
}
void solve()
{
int i, j, k, n, res, t;
memset(a, 0, sizeof(a));
scanf("%d", &n);
k = 0;
for(i = 0; i < n; i ++)
{
scanf("%d", &a[i]);
k += a[i];
}
if(!k)
{
printf("1\n");
return ;
}
for(i = 3, k = 0; i >= 0; i --)
k = k * 8 + a[i];
res = 0;
for(i = 0; i < n; i ++)
for(j = 1; j <= 3 && j <= a[i]; j ++)
{
t = (((k * 4 + j) * 4 + i) * 4 + j) * 4 + i;
res += dp(t);
}
printf("%d\n", res);
}
int main()
{
int t;
memset(f, -1, sizeof(f));
scanf("%d", &t);
while(t --)
solve();
return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.