zoukankan      html  css  js  c++  java
  • 【蓝桥杯】历届试题 波动数列(运行超时)

      历届试题 波动数列  
    时间限制:1.0s   内存限制:256.0MB
        
    问题描述
      观察这个数列:
      1 3 0 2 -1 1 -2 ...

      这个数列中后一项总是比前一项增加2或者减少3。

      栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a或者减少b的整数数列可能有多少种呢?
    输入格式
      输入的第一行包含四个整数 n s a b,含义如前面说述。
    输出格式
      输出一行,包含一个整数,表示满足条件的方案数。由于这个数很大,请输出方案数除以100000007的余数。
    样例输入
    4 10 2 3
    样例输出
    2
    样例说明
      这两个数列分别是2 4 1 3和7 4 1 -2。
    数据规模和约定
      对于10%的数据,1<=n<=5,0<=s<=5,1<=a,b<=5;
      对于30%的数据,1<=n<=30,0<=s<=30,1<=a,b<=30;
      对于50%的数据,1<=n<=50,0<=s<=50,1<=a,b<=50;
      对于70%的数据,1<=n<=100,0<=s<=500,1<=a, b<=50;
      对于100%的数据,1<=n<=1000,-1,000,000,000<=s<=1,000,000,000,1<=a, b<=1,000,000。
     
    Java源代码:
     1 // 来自 http://www.lai18.com/content/2204109.html
     2 import java.util.Scanner;
     3 
     4 public class Main {
     5     static int MAXN = 1100;
     6     static int MOD = 100000007;
     7 
     8     static int[][] F = new int[2][MAXN * MAXN];
     9     static int e = 0;
    10     static long n, s, a, b;
    11 
    12     static void DP() {
    13         F[e][0] = 1;
    14         for (int i = 1; i < n; i++) {
    15             e = 1 - e;
    16             for (int j = 0; j <= i * (i + 1) / 2; j++) {
    17                 if (i > j)
    18                     F[e][j] = F[1 - e][j];
    19                 else
    20                     F[e][j] = (F[1 - e][j] + F[1 - e][j - i]) % MOD;
    21             }
    22         }
    23     }
    24 
    25     public static void main(String[] args) {
    26         Scanner in = new Scanner(System.in);
    27         n = in.nextLong();
    28         s = in.nextLong();
    29         a = in.nextLong();
    30         b = in.nextLong();
    31         int count = 0;
    32         long i, t;
    33         DP();
    34         for (i = 0; i <= n * (n - 1) / 2; i++) {
    35             t = s - i * a + (n * (n - 1) / 2 - i) * b;
    36             if (t % n == 0)
    37                 count = (count + F[e][(int) i]) % MOD;
    38         }
    39         System.out.println(count);
    40     }
    41 }
    评测点序号评测结果得分CPU使用内存使用下载评测数据
    1 正确 10.00 140ms 32.52MB 输入 输出
    2 正确 10.00 140ms 32.50MB 输入 输出
    3 正确 10.00 140ms 32.54MB 输入 输出
    4 正确 10.00 202ms 32.59MB 输入 输出
    5 正确 10.00 171ms 32.63MB 输入 输出
    6 正确 10.00 202ms 32.49MB 输入 输出
    7 正确 10.00 140ms 32.79MB 输入 输出
    8 正确 10.00 920ms 32.91MB 输入 输出
    9 运行超时 0.00 运行超时 32.88MB 输入 输出
    10 运行超时 0.00 运行超时 32.75MB 输入 输出
  • 相关阅读:
    动态规划——Best Time to Buy and Sell Stock IV
    动态规划——Split Array Largest Sum
    动态规划——Burst Ballons
    动态规划——Best Time to Buy and Sell Stock III
    动态规划——Edit Distance
    动态规划——Longest Valid Parentheses
    动态规划——Valid Permutations for DI Sequence
    构建之法阅读笔记05
    构建之法阅读笔记04
    构建之法阅读笔记03
  • 原文地址:https://www.cnblogs.com/wuqianling/p/5370066.html
Copyright © 2011-2022 走看看