24h購物| | PChome| 登入
2013-12-05 16:22:15| 人氣2,720| 回應0 | 上一篇 | 下一篇

[UVA][dp] 11654 - Arithmetic Subsequence

推薦 0 收藏 0 轉貼0 訂閱站台

I

Arithmetic Subsequence

Input: Standard Input

Output: Standard Output

                                   

A sequence of numbers is said to be arithmetic if there is a constant difference between successive elements. For example {1,5,9} is an arithmetic sequence where the difference between each term is 4 and the sequence contains three elements. In this problem, given a set of numbers, you are to determine how many proper subset of this set is an arithmetic sequence. The elements in the subset must be in the same order as they occur in the superset. That is for the superset { 1,3,6},  {1,3} is a subset but not {3,1}.

 

Input

The first line of input will be a positive integer T<=100, where T denotes the number of test cases. Each case starts with a positive integer n<=250. This is followed by n non negative integers each on a new line.  The value of these integers will be less 100000001. The n integers represent the elements of the superset.

 

Output

For each case of input there will be one line of output. It will be the number of subsets which form an arithmetic sequence as described earlier. The numbers maybe very large, therefore you are to give the answer modulo 10000007. Follow sample output for exact format.

 

Sample Input                              Output for Sample Input

3

3

1

2

4

1

1

3

1

2

2

 

 

Case 1: 6

Case 2: 1

Case 3: 6

Problem setter: Shamim Hafiz, Special Thanks: Md. Arifuzzaman Arif

題目描述:

在序列中,挑出子序列為等差的個數為何 ?

題目解法:


dp[i][j] 表示等比數列的最後兩項為 x[i](最後一項) 和 x[j](倒數第二項) 的個數。

因此 dp[i][j] = 1 + sigma(dp[k][j]) // 其中要符合 x[j]-x[k] == x[i]-x[j]

#include <stdio.h>
#include <string.h>

int main() {
    int testcase, cases = 0;
    int n, i, j, k, x[255];
    int dp[255][255], ret;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d", &n);
        for(i = 0; i < n; i++)
            scanf("%d", &x[i]);
        ret = n;// all |S| = 1
        for(i = 0; i < n; i++) {
            for(j = i-1; j >= 0; j--) {
                int d = x[i]-x[j];
                int &r = dp[i][j] = 1;// {x[j], x[i]}
                for(k = j-1; k >= 0; k--) {
                    if(x[j]-x[k] == d) {
                        r += dp[j][k];
                        if(r >= 10000007)
                            r -= 10000007;
                    }
                }
                ret += r;
                if(ret >= 10000007)
                    ret -= 10000007;
            }
        }
        printf("Case %d: %d\n", ++cases, ret);
    }
    return 0;
}

台長: Morris
人氣(2,720) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][排容原理] 11806 - Cheerleaders
此分類上一篇:[UVA][數學] 11648 - Divide The Land

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文