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;
}


 

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