HDU 5900 - QSC and Master

Problem Description
Every school has some legends, Northeastern University is the same.

Enter from the north gate of Northeastern University,You are facing the main building of Northeastern University.Ninety-nine percent of the students have not been there,It is said that there is a monster in it.

QSCI am a curious NEU_ACMer,This is the story he told us.

It’s a certain period,QSCI am in a dark night, secretly sneaked into the East Building,hope to see the master.After a serious search,He finally saw the little master in a dark corner. The master said:

“You and I, we're interfacing.please solve my little puzzle!

There are N pairs of numbers,Each pair consists of a key and a value,Now you need to move out some of the pairs to get the score.You can move out two continuous pairs,if and only if their keys are non coprime(their gcd is not one).The final score you get is the sum of all pair’s value which be moved out. May I ask how many points you can get the most?

The answer you give is directly related to your final exam results~The young man~”

QSC is very sad when he told the story,He failed his linear algebra that year because he didn't work out the puzzle.

Could you solve this puzzle?

(Data range:1<=N<=300
1<=Ai.key<=1,000,000,000
0<Ai.value<=1,000,000,000)



Input
First line contains a integer T,means there are T(1≤T≤10) test case。

  Each test case start with one integer N . Next line contains N integers,means Ai.key.Next line contains N integers,means Ai.value.



Output
For each test case,output the max score you could get in a line.


Sample Input
3
3
1 2 3
1 1 1
3
1 2 4
1 1 1
4
1 3 4 3
1 1 1 1
 

Sample Output

0
2
0

题意:给出几个数字的键值和值,相邻的数字键值之间的GCD如果大于1则可以合并,合并可以得到的价值是这两个数的值的和,同时抹去这两个数字。给出 T 组测试数据,每组测试数据有一个 n 代表 n 个数,第一行 n 个数是键值,下面一行是值。

分别存下每个数个键值和值,先把每个区间的值的和存下,然后用 gcd[i][j] 数组来表示 i 到 j 之间是否都能合并,注意处理好边界,如果不能就从中枚举断点找到最大的。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

long long key[300 + 5], val[300 + 5];
bool gcd[300 + 5][300 + 5];
long long sum[300 + 5];
long long ans[300 + 5][300 + 5];

long long GCD(long long a, long long b)
{
    if (a < b)
    {
        int t = a;
        a = b;
        b = t;
    }
    int tmp;
    while (b != 0)
    {
        tmp = b;
        b = a%b;
        a = tmp;
    }
    return tmp;
}

int main()
{
    int T;
    scanf("%d", &T);

    while (T--)
    {
        int n;
        scanf("%d", &n);

        memset(gcd, false, sizeof(gcd));
        memset(sum, 0, sizeof(sum));
        memset(ans, 0, sizeof(ans));

        for (int i = 0; i < n; ++i)
            scanf("%lld", &key[i]);
        for (int i = 0; i < n; ++i)
        {
            scanf("%lld", &val[i]);
            sum[i] = sum[i - 1] + val[i];
        }

        for (int i = 0; i < n; ++i)
            if (GCD(key[i], key[i-1]) > 1)
                gcd[i][i-1] = true;
        for (int len = 2; len <= n; len += 2)
        {
            for (int i = 0; i + len - 1 < n; ++i)
            {
                int left = i;
                int right = i + len - 1;

                if (GCD(key[left], key[right]) > 1 && gcd[left + 1][right - 1])
                    gcd[left][right] = true;
                if (GCD(key[left], key[left + 1]) > 1 && gcd[left + 2][right])
                    gcd[left][right] = true;
                if (GCD(key[right], key[right - 1]) > 1 && gcd[left][right - 2])
                    gcd[left][right] = true;
            }
        }
        for (int len = 2; len <= n; len++)
        {
            for (int i = 0; i + len - 1 < n; ++i)
            {
                int left = i;
                int right = i + len - 1;
                if (gcd[left][right])
                    ans[left][right] = sum[right] - sum[left - 1];
                else
                {
                    for (int j = left; j < right; ++j)
                        ans[left][right] = max(ans[left][right], ans[left][j] + ans[j + 1][right]);
                }
            }
        }
        printf("%lld\n", ans[0][n-1]);
    }
    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章