Submission #110597

# Submission time Handle Problem Language Result Execution time Memory
110597 2019-05-11T09:36:51 Z Pro_ktmr Boat (APIO16_boat) C++14
58 / 100
2000 ms 12484 KB
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")

#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define PB push_back
#define MP make_pair

const LL MOD = 1000000007;

//累乗 O(log N)
long long power(long long x, int N){
	if(N == 1) return x;
	long long tmp = power(x, N/2);
	if(N%2 == 0) return tmp * tmp % MOD;
	else return tmp * tmp % MOD * x % MOD;
}

//逆元 O(log x)
long long inverse(long long x){
	return power(x, MOD-2);
}

//コンビネーション
struct Combination{
private:
	int N;
	vector<long long> fact, inv;
public:
	void init(int n){ //初期化する O(N)
		N = n;
		fact.push_back(1);
		inv.push_back(1);
		for(long long i=1; i<=N; i++){
			fact.push_back(fact.back()*i%MOD);
			inv.push_back(inverse(fact.back()));
		}
	}
	long long comb(int n, int k){ //nCkを求める O(1)
		return fact[n] * inv[k] % MOD * inv[n-k] % MOD;
	}
};

Combination comb;

int N, a[500], b[500], A[500], B[500];
vector<int> v;
vector<int> ls[1000];

LL memo[1000][501];
LL culc(LL range, int num){
	if(num == 1) return range;
	LL ret = 0;
	LL tmp1 = range;
	LL tmp2 = range-1;
	for(int i=0; i<=num-2; i++){ //中のを何個使うか
		if(tmp2 == 0) break;
		tmp1 = tmp1 * tmp2 % MOD;
		tmp1 = tmp1 * inverse(i+2) % MOD;
		tmp2--;
		ret += tmp1 * comb.comb(num-2, i) % MOD;
	}
	return ret % MOD;
}

//a:=区間、b:=学校
LL dp[1000][500][2];
LL solve(int a, int b, int ok){
	if(a == v.size()-1) return 1;
	if(ok == 0 && b == N) return 0;
	if(ok == 1 && b == N) return 1;
	if(dp[a][b][ok] != -1) return dp[a][b][ok];
	if(ok == 0){
		dp[a][b][ok] = solve(a, b+1, 0);
		if(A[b] <= a && a < B[b]){
			int idx = lower_bound(ls[a].begin(), ls[a].end(), b) - ls[a].begin();
			for(int i=idx; i<ls[a].size()-1; i++){
				dp[a][b][ok] += memo[a][i-idx+1]*solve(a+1, ls[a][i]+1, 1);
				dp[a][b][ok] %= MOD;
			}
		}
	}
	else{
		dp[a][b][ok] = solve(a+1, b, 1);
		dp[a][b][ok] += solve(a, b, 0);
		dp[a][b][ok] %= MOD;
	}
	return dp[a][b][ok];
}

int main(){
	scanf("%d", &N);
	for(int i=0; i<N; i++){
		scanf("%d%d", a+i, b+i);
		b[i]++;
		v.PB(a[i]);
		v.PB(b[i]);
	}
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	for(int i=0; i<N; i++){
		A[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
		B[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin();
		for(int j=A[i]; j<B[i]; j++) ls[j].PB(i);
	}
	comb.init(N);
	for(int i=0; i<v.size(); i++){
		ls[i].PB(N);
		for(int j=0; j<N; j++) dp[i][j][0] = -1;
		for(int j=0; j<N; j++) dp[i][j][1] = -1;
		if(i != v.size()-1) for(int j=1; j<=ls[i].size(); j++) memo[i][j] = culc(v[i+1]-v[i], j);
	}
	printf("%d\n", (solve(0, 0, 1)-1+MOD)%MOD);
}

Compilation message

boat.cpp: In function 'long long int solve(int, int, int)':
boat.cpp:71:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(a == v.size()-1) return 1;
     ~~^~~~~~~~~~~~~
boat.cpp:79:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int i=idx; i<ls[a].size()-1; i++){
                   ~^~~~~~~~~~~~~~~
boat.cpp: In function 'int main()':
boat.cpp:109:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<v.size(); i++){
               ~^~~~~~~~~
boat.cpp:113:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i != v.size()-1) for(int j=1; j<=ls[i].size(); j++) memo[i][j] = culc(v[i+1]-v[i], j);
      ~~^~~~~~~~~~~~~
boat.cpp:113:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(i != v.size()-1) for(int j=1; j<=ls[i].size(); j++) memo[i][j] = culc(v[i+1]-v[i], j);
                                    ~^~~~~~~~~~~~~~
boat.cpp:115:43: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
  printf("%d\n", (solve(0, 0, 1)-1+MOD)%MOD);
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~^
boat.cpp:94:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
boat.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", a+i, b+i);
   ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 25 ms 12288 KB Output is correct
2 Correct 29 ms 12288 KB Output is correct
3 Correct 31 ms 12160 KB Output is correct
4 Correct 25 ms 12288 KB Output is correct
5 Correct 28 ms 12288 KB Output is correct
6 Correct 23 ms 12360 KB Output is correct
7 Correct 22 ms 12288 KB Output is correct
8 Correct 22 ms 12288 KB Output is correct
9 Correct 21 ms 12288 KB Output is correct
10 Correct 19 ms 12288 KB Output is correct
11 Correct 22 ms 12288 KB Output is correct
12 Correct 27 ms 12288 KB Output is correct
13 Correct 23 ms 12288 KB Output is correct
14 Correct 23 ms 12288 KB Output is correct
15 Correct 20 ms 12288 KB Output is correct
16 Correct 8 ms 2560 KB Output is correct
17 Correct 9 ms 2688 KB Output is correct
18 Correct 6 ms 2560 KB Output is correct
19 Correct 6 ms 2688 KB Output is correct
20 Correct 6 ms 2560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 12288 KB Output is correct
2 Correct 29 ms 12288 KB Output is correct
3 Correct 31 ms 12160 KB Output is correct
4 Correct 25 ms 12288 KB Output is correct
5 Correct 28 ms 12288 KB Output is correct
6 Correct 23 ms 12360 KB Output is correct
7 Correct 22 ms 12288 KB Output is correct
8 Correct 22 ms 12288 KB Output is correct
9 Correct 21 ms 12288 KB Output is correct
10 Correct 19 ms 12288 KB Output is correct
11 Correct 22 ms 12288 KB Output is correct
12 Correct 27 ms 12288 KB Output is correct
13 Correct 23 ms 12288 KB Output is correct
14 Correct 23 ms 12288 KB Output is correct
15 Correct 20 ms 12288 KB Output is correct
16 Correct 8 ms 2560 KB Output is correct
17 Correct 9 ms 2688 KB Output is correct
18 Correct 6 ms 2560 KB Output is correct
19 Correct 6 ms 2688 KB Output is correct
20 Correct 6 ms 2560 KB Output is correct
21 Correct 318 ms 12156 KB Output is correct
22 Correct 283 ms 12484 KB Output is correct
23 Correct 241 ms 12024 KB Output is correct
24 Correct 286 ms 12148 KB Output is correct
25 Correct 304 ms 12024 KB Output is correct
26 Correct 408 ms 12024 KB Output is correct
27 Correct 423 ms 12380 KB Output is correct
28 Correct 385 ms 12024 KB Output is correct
29 Correct 398 ms 11976 KB Output is correct
30 Correct 21 ms 12288 KB Output is correct
31 Correct 26 ms 12288 KB Output is correct
32 Correct 22 ms 12288 KB Output is correct
33 Correct 24 ms 12288 KB Output is correct
34 Correct 21 ms 12288 KB Output is correct
35 Correct 28 ms 12288 KB Output is correct
36 Correct 23 ms 12288 KB Output is correct
37 Correct 27 ms 12288 KB Output is correct
38 Correct 27 ms 12288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 2424 KB Output is correct
2 Correct 28 ms 2424 KB Output is correct
3 Correct 36 ms 2348 KB Output is correct
4 Correct 40 ms 2296 KB Output is correct
5 Correct 42 ms 2424 KB Output is correct
6 Correct 83 ms 2296 KB Output is correct
7 Correct 90 ms 2296 KB Output is correct
8 Correct 89 ms 2284 KB Output is correct
9 Correct 81 ms 2296 KB Output is correct
10 Correct 86 ms 2296 KB Output is correct
11 Correct 40 ms 2340 KB Output is correct
12 Correct 31 ms 2296 KB Output is correct
13 Correct 35 ms 2296 KB Output is correct
14 Correct 39 ms 2304 KB Output is correct
15 Correct 47 ms 2296 KB Output is correct
16 Correct 19 ms 1464 KB Output is correct
17 Correct 16 ms 1536 KB Output is correct
18 Correct 17 ms 1536 KB Output is correct
19 Correct 18 ms 1536 KB Output is correct
20 Correct 21 ms 1536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 12288 KB Output is correct
2 Correct 29 ms 12288 KB Output is correct
3 Correct 31 ms 12160 KB Output is correct
4 Correct 25 ms 12288 KB Output is correct
5 Correct 28 ms 12288 KB Output is correct
6 Correct 23 ms 12360 KB Output is correct
7 Correct 22 ms 12288 KB Output is correct
8 Correct 22 ms 12288 KB Output is correct
9 Correct 21 ms 12288 KB Output is correct
10 Correct 19 ms 12288 KB Output is correct
11 Correct 22 ms 12288 KB Output is correct
12 Correct 27 ms 12288 KB Output is correct
13 Correct 23 ms 12288 KB Output is correct
14 Correct 23 ms 12288 KB Output is correct
15 Correct 20 ms 12288 KB Output is correct
16 Correct 8 ms 2560 KB Output is correct
17 Correct 9 ms 2688 KB Output is correct
18 Correct 6 ms 2560 KB Output is correct
19 Correct 6 ms 2688 KB Output is correct
20 Correct 6 ms 2560 KB Output is correct
21 Correct 318 ms 12156 KB Output is correct
22 Correct 283 ms 12484 KB Output is correct
23 Correct 241 ms 12024 KB Output is correct
24 Correct 286 ms 12148 KB Output is correct
25 Correct 304 ms 12024 KB Output is correct
26 Correct 408 ms 12024 KB Output is correct
27 Correct 423 ms 12380 KB Output is correct
28 Correct 385 ms 12024 KB Output is correct
29 Correct 398 ms 11976 KB Output is correct
30 Correct 21 ms 12288 KB Output is correct
31 Correct 26 ms 12288 KB Output is correct
32 Correct 22 ms 12288 KB Output is correct
33 Correct 24 ms 12288 KB Output is correct
34 Correct 21 ms 12288 KB Output is correct
35 Correct 28 ms 12288 KB Output is correct
36 Correct 23 ms 12288 KB Output is correct
37 Correct 27 ms 12288 KB Output is correct
38 Correct 27 ms 12288 KB Output is correct
39 Correct 39 ms 2424 KB Output is correct
40 Correct 28 ms 2424 KB Output is correct
41 Correct 36 ms 2348 KB Output is correct
42 Correct 40 ms 2296 KB Output is correct
43 Correct 42 ms 2424 KB Output is correct
44 Correct 83 ms 2296 KB Output is correct
45 Correct 90 ms 2296 KB Output is correct
46 Correct 89 ms 2284 KB Output is correct
47 Correct 81 ms 2296 KB Output is correct
48 Correct 86 ms 2296 KB Output is correct
49 Correct 40 ms 2340 KB Output is correct
50 Correct 31 ms 2296 KB Output is correct
51 Correct 35 ms 2296 KB Output is correct
52 Correct 39 ms 2304 KB Output is correct
53 Correct 47 ms 2296 KB Output is correct
54 Correct 19 ms 1464 KB Output is correct
55 Correct 16 ms 1536 KB Output is correct
56 Correct 17 ms 1536 KB Output is correct
57 Correct 18 ms 1536 KB Output is correct
58 Correct 21 ms 1536 KB Output is correct
59 Execution timed out 2045 ms 7272 KB Time limit exceeded
60 Halted 0 ms 0 KB -