main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. // MOD ...
  8. const MOD = 1000000007
  9. func countIncreasing(scanner *bufio.Scanner) []int {
  10. caseCnt := ReadInt(scanner)
  11. answer := make([]int, caseCnt)
  12. for cid := 0; cid < caseCnt; cid++ {
  13. println("Case", cid+1, "/", caseCnt)
  14. params := ReadInts(scanner)
  15. n, m, X, Y, Z := params[0], params[1], params[2], params[3], params[4]
  16. A := make([]int, m)
  17. limits := make([]int, n)
  18. for i := 0; i < n; i++ {
  19. if i < m {
  20. A[i] = ReadInt(scanner)
  21. }
  22. limits[i] = A[i%m]
  23. A[i%m] = (X*A[i%m] + Y*(i+1)) % Z
  24. } // To generate input array 'limits'
  25. st := NewSegmentTree(limits)
  26. dp := make([]int, n+1)
  27. dp[0] = 1
  28. for i := 1; i <= n; i++ {
  29. dp[i]++
  30. // *********************************
  31. // * Segment tree solution *
  32. // *********************************
  33. for j := st.NextGreater(limits[i-1], i) + 1; j != 0; j = st.NextGreater(limits[i-1], j) + 1 {
  34. dp[j] += dp[i]
  35. dp[j] %= MOD
  36. }
  37. // *********************************
  38. // * Original solution *
  39. // *********************************
  40. // for j := i + 1; j <= n; j++ { // It is too slow to search between i+1 and n for big case
  41. // if limits[i-1] < limits[j-1] {
  42. // dp[j] += dp[i]
  43. // dp[j] %= MOD
  44. // }
  45. // }
  46. // *********************************
  47. // Both are to slow for solving the big problem set.
  48. }
  49. for i := 1; i <= n; i++ {
  50. answer[cid] += dp[i]
  51. }
  52. answer[cid] %= MOD
  53. }
  54. return answer
  55. }
  56. func main() {
  57. inputFiles := []string{"C-small-practice.in", "C-large-practice.in", "test.in"}
  58. outputFiles := []string{"result-small.out", "result-large.out", "test.out"}
  59. const (
  60. small = iota
  61. large
  62. test
  63. )
  64. fileType := small
  65. fin, _ := os.Open(inputFiles[fileType])
  66. defer fin.Close()
  67. scanner := bufio.NewScanner(fin)
  68. answer := countIncreasing(scanner)
  69. fout, _ := os.Create(outputFiles[fileType])
  70. defer fout.Close()
  71. for i, v := range answer {
  72. s := fmt.Sprintf("Case #%d: %d\n", i+1, v)
  73. fout.WriteString(s)
  74. }
  75. }