Problem F Removal Game
Bobby Roberts is totally bored in his algorithms class, so he’s developed a little solitaire game. He writes down a sequence of positive integers and then begins removing them one at a time. The cost of each removal is equal to the greatest common divisor (gcd) of the two surrounding numbers (wrapping around either end if necessary). For example, if the sequence of numbers was 2, 3, 4, 5 he could remove the 3 at a cost of 2 (= gcd(2,4)) or he could remove the 4 at a cost of 1 (= gcd(3,5)). The cost of removing 2 would be 1 and the removal of 5 would cost 2. Note that if the 4 is removed first, the removal of the 3 afterwards now has a cost of only 1. Bobby keeps a running total of each removal cost. When he ends up with just two numbers remaining he takes their gcd, adds that cost to the running total, and ends the game by removing them both. The object of the game is to remove all of the numbers at the minimum total cost. Unfortunately, he spent so much time in class on this game, he didn’t pay attention to several important lectures which would lead him to an algorithm to solve this problem. Since none of you have ever wasted time in your algorithm classes, I’m sure you’ll have no problem finding the minimum cost given any sequence of numbers.
Input
Input contains multiple test cases. Each test case consists of a single line starting with an integer n whichindicates thenumber ofvaluesin thesequence (2 ≤ n ≤ 100). This isfollowed by n positive integers which make up the sequence of values in the game. All of these integers will be≤ 1000. Input terminates with a line containing a single 0. There are at most 1000 test cases.
Output
For each test case, display the minimum cost of removing all of the numbers.
Sample Input 1

4 2 3 4 5

5 14 2 4 6 8

0

Sample Output 1

3

8

题意:给你一个长度为n的序列 每删除一个数的代价为与他相邻的的两个数的gcd 注意是一个循环的序列 把这个序列首尾相接考虑

题解:参看tyvj1056 写法稍微不同,思想类似。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define esp 0.00000000001
 5 const int N=1e3+10,M=1e6+10,inf=1e9+10,mod=1000000007;
 6 int gcd(int a,int  b)
 7 {
 8     return b==0?a:gcd(b,a%b);
 9 }
10 ll a[N];
11 ll dp[N][N];
12 ll ff[1005][1005];
13 int main()
14 {
15     ll x,i,t;
16     for(int i=1;i<=1000;i++)
17     {
18         for(int j=1;j<=1000;j++)
19         {
20             ff[i][j]=gcd(i,j);
21         }
22     }
23     while(scanf("%I64d",&x)!=EOF)
24     {
25         if(x==0)
26             break;
27         for(i=1; i<=x; i++)
28             scanf("%I64d",&a[i]),a[i+x]=a[i];
29         for(int i=1; i<=2*x; i++)
30         {
31             dp[i][i]=0;
32             for(int j=i+1; j<=2*x; j++)
33             {
34                 dp[i][j]=1000000000;
35             }
36         }
37         for(t=1; t<=x; t++)
38         {
39             for(i=1; i+t<2*x; i++)
40             {
41                 for(ll k=i; k<t+i; k++)
42                 {
43                     if((i+x)==(t+i+1))//终态只剩下两个 直接求gcd 更新
44                         dp[i][i+t]=min(dp[i][i+t],dp[i][k]+dp[k+1][t+i]+ff[a[i]][a[k+1]]);
45                     else
46                         dp[i][i+t]=min(dp[i][i+t],dp[i][k]+dp[k+1][t+i]+ff[a[i]][a[t+i+1]]);
47                 }
48             }
49         }
50         ll ans=1000000000;
51         for(i=1; i<=x; i++)
52             ans=min(ans,dp[i][i+x-1]);
53         printf("%I64d\n",ans);
54     }
55     return 0;
56 }