QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#253132#7308. Permutation and noitatumrePPetroTarnavskyi#AC ✓61ms3420kbC++202.4kb2023-11-16 18:31:382023-11-16 18:31:38

Judging History

This is the latest submission verdict.

  • [2023-11-16 18:31:38]
  • Judged
  • Verdict: AC
  • Time: 61ms
  • Memory: 3420kb
  • [2023-11-16 18:31:38]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); i--)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int mod = 1e9 + 7;
const int M = 4;

void updAdd(int& a, int b)
{
	a += b;
	if (a >= mod)
		a -= mod;
}
int add(int a, int b)
{
	return a + b < mod ? a + b : a + b - mod;
}

int mult(int a, int b)
{
	return (LL) a * b % mod;
}

struct Matrix
{
	int a[M][M];
	
	Matrix()
	{
		FOR (i, 0, M)
			fill(a[i], a[i] + M, 0);
	}
	
	Matrix(int x)
	{
		FOR (i, 0, M)
		{
			FOR (j, 0, M)
			{
				a[i][j] = i == j ? x : 0;
			}
		}
	}
	
	Matrix operator *(const Matrix& m)
	{
		Matrix res;
		FOR (i, 0, M)
		{
			FOR (j, 0, M)
			{
				FOR (k, 0, M)
				{
					updAdd(res.a[i][j], mult(a[i][k], m.a[k][j]));
				}
			}
		}
		return res;
	}
};

Matrix binpow(Matrix a, int n)
{
	Matrix res(1);
	while(n)
	{
		if(n & 1)
			res = res * a;
		a = a * a;
		n /= 2;
	}
	return res;
}


/*
bool ok(VI p)
{
	int n = SZ(p);
	p.resize(2 * n);
	FOR(i, 0, n)
		p[2 * n - 1 - i] = p[i];
	
	FOR(a, 0, 2 * n)
	FOR(b, a + 1, 2 * n)
	FOR(c, b + 1, 2 * n)
	FOR(d, c + 1, 2 * n)
	{
		if(p[a] < p[c] && p[c] < p[d] && p[d] < p[b])
			return 0;
	}
	return 1;
}

void solve(int n)
{
	VI p(n);
	iota(ALL(p), 0);
	
	cout << n << endl;
	cerr << n << endl;
	
	int cnt = 0;
	do
	{
		if(ok(p))
		{
			FOR(i, 0, n)
				cout << p[i] << " ";
			cout << endl;
			cnt++;
		}
	}while(next_permutation(ALL(p)));
	
	cout << endl;
	cerr << cnt << endl;
}
*/

int main()
{
    ios::sync_with_stdio(0); 
    cin.tie(0);
	
	Matrix A;
	A.a[0][0] = 2;
	A.a[0][2] = 1;
	A.a[0][3] = 2;
	A.a[1][0] = 1;
	A.a[2][1] = 1;
	A.a[3][3] = 1;
	
	
	VI a = {1, 1, 2, 6, 16};
	
	
	int n;
	while(cin >> n)
	{
		if(n < SZ(a))
		{
			cout << a[n] << "\n";
			continue;
		}
		Matrix B = binpow(A, n - 4);
		
		int res = 0;
		res = add(res, mult(B.a[0][0], 16));
		res = add(res, mult(B.a[0][1], 6));
		res = add(res, mult(B.a[0][2], 2));
		res = add(res, mult(B.a[0][3], 1));
		
		cout << res << "\n";
	}
	
	



	
	
	return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3420kb

input:

4
1000000000

output:

16
861159011

result:

ok 2 number(s): "16 861159011"

Test #2:

score: 0
Accepted
time: 61ms
memory: 3376kb

input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
...

output:

1
2
6
16
36
80
178
394
870
1920
4236
9344
20610
45458
100262
221136
487732
1075728
2372594
5232922
11541574
25455744
56144412
123830400
273116546
602377506
328585407
930287362
462952218
254489838
439267033
341486279
937462398
314191817
969869915
877202216
68596237
107062384
91326979
251250197
609562...

result:

ok 20000 numbers

Extra Test:

score: 0
Extra Test Passed