forked from watashi/AlgoSolution
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
const int MAXN = 51; | ||
|
||
int r, c; | ||
long long a[MAXN][MAXN], s[MAXN][MAXN]; | ||
long long dp[MAXN][MAXN][MAXN][MAXN]; | ||
|
||
struct Rect { | ||
union { | ||
int p[4]; | ||
struct { | ||
int r1, r2, c1, c2; | ||
} q; | ||
}; | ||
|
||
Rect(int r1, int r2, int c1, int c2) { | ||
q.r1 = r1; | ||
q.r2 = r2; | ||
q.c1 = c1; | ||
q.c2 = c2; | ||
} | ||
|
||
long long& dp() const { | ||
return ::dp[q.r1][q.r2][q.c1][q.c2]; | ||
} | ||
|
||
long long sum() const { | ||
return s[q.r1][q.c1] - s[q.r1][q.c2] - s[q.r2][q.c1] + s[q.r2][q.c2]; | ||
} | ||
|
||
int r() const { | ||
return q.r2 - q.r1; | ||
} | ||
|
||
int c() const { | ||
return q.c2 - q.c1; | ||
} | ||
}; | ||
|
||
const int d[4] = {1, -1, 1, -1}; | ||
|
||
long long gao(Rect s) { | ||
long long& ret = s.dp(); | ||
if (ret == -1) { | ||
if (s.r() == 0 || s.c() == 0) { | ||
ret = 0; | ||
} else { | ||
int t = (r + c) - (s.r() + s.c()); | ||
long long sum = s.sum(); | ||
if (t % 2 == 0) { | ||
int k = -1; | ||
long long cmp = 0; | ||
for (int i = 0; i < 4; ++i) { | ||
s.p[i] += d[i]; | ||
long long sub = s.sum(); | ||
if (k == -1 || cmp > sum - sub) { | ||
k = i; | ||
cmp = sum - sub; | ||
} | ||
s.p[i] -= d[i]; | ||
} | ||
s.p[k] += d[k]; | ||
ret = gao(s); | ||
} else { | ||
for (int i = 0; i < 4; ++i) { | ||
s.p[i] += d[i]; | ||
long long sub = s.sum(); | ||
ret = max(ret, (sum - sub) + gao(s)); | ||
s.p[i] -= d[i]; | ||
} | ||
} | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
int main() { | ||
int re; | ||
long long ans; | ||
|
||
scanf("%d", &re); | ||
for (int ri = 1; ri <= re; ++ri) { | ||
scanf("%d%d", &r, &c); | ||
for (int i = 0; i < r; ++i) { | ||
for (int j = 0; j < c; ++j) { | ||
scanf("%lld", &a[i][j]); | ||
} | ||
} | ||
memset(s, 0, sizeof(s)); | ||
for (int i = 1; i <= r; ++i) { | ||
for (int j = 1; j <= c; ++j) { | ||
s[i][j] = s[i][j - 1] + a[i - 1][j - 1]; | ||
} | ||
for (int j = 1; j <= c; ++j) { | ||
s[i][j] += s[i - 1][j]; | ||
} | ||
} | ||
memset(dp, -1, sizeof(dp)); | ||
ans = gao(Rect(0, r, 0, c)); | ||
if (ans * 2 == s[r][c]) { | ||
ans = s[r][c]; | ||
} else { | ||
ans = max(ans, s[r][c] - ans); | ||
} | ||
printf("%lld\n", ans); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
//Correct Answer | ||
//Execution Time: 9.27 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#include <cstdio> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
typedef long long int64; | ||
|
||
struct Matrix { | ||
static const int MAXN = 128; | ||
|
||
int r, c; | ||
int64 a[MAXN][MAXN]; | ||
|
||
int64* operator[](int i) { | ||
return a[i]; | ||
} | ||
|
||
const int64* operator[](int i) const { | ||
return a[i]; | ||
} | ||
|
||
void init(int n) { | ||
init(n, n, 0); | ||
for (int i = 0; i < n; ++i) { | ||
a[i][i] = 1; | ||
} | ||
} | ||
|
||
void init(int r, int c) { | ||
this->r = r; | ||
this->c = c; | ||
} | ||
|
||
void init(int r, int c, int64 x) { | ||
init(r, c); | ||
for (int i = 0; i < r; ++i) { | ||
fill(a[i], a[i] + c, x); | ||
} | ||
} | ||
|
||
void init(const Matrix& o) { | ||
init(o.r, o.c); | ||
for (int i = 0; i < r; ++i) { | ||
copy(o[i], o[i] + c, a[i]); | ||
} | ||
} | ||
}; | ||
|
||
void mul(const Matrix& a, const Matrix& b, int64 m, Matrix& ret) { | ||
static Matrix c; | ||
c.init(a.r, b.c); | ||
for (int i = 0; i < c.r; ++i) { | ||
for (int j = 0; j < c.c; ++j) { | ||
int64 x = 0; | ||
for (int k = 0; k < a.c; ++k) { | ||
x += a[i][k] * b[k][j] % m; | ||
} | ||
c[i][j] = x % m; | ||
} | ||
} | ||
ret.init(c); | ||
} | ||
|
||
void pow(const Matrix& a, int64 b, int64 m, Matrix& ret) { | ||
static Matrix c; | ||
c.init(a); | ||
|
||
ret.init(c.r); | ||
while (b > 0) { | ||
if (b % 2 != 0) { | ||
mul(ret, c, m, ret); | ||
} | ||
b /= 2; | ||
mul(c, c, m, c); | ||
} | ||
} | ||
|
||
void init(int n, Matrix& a) { | ||
int m = (1 << (n - 1)) - 1; | ||
a.init(1 << n, 1 << n, 0); | ||
for (int i = 0; i < a.r; ++i) { | ||
for (int j = 0; j < a.c; ++j) { | ||
if ((m & ((i ^ (i >> 1)) | (i ^ (j >> 1)) | (i ^ j))) == m) { | ||
a[i][j] = 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
const int64 MOD = 1000000007; | ||
Matrix a; | ||
|
||
int main() { | ||
int n; | ||
long long m; | ||
|
||
scanf("%d%lld", &n, &m); | ||
init(n, a); | ||
pow(a, m - 1, MOD, a); | ||
m = 0; | ||
for (int i = 0; i < a.r; ++i) { | ||
for (int j = 0; j < a.c; ++j) { | ||
m += a[i][j]; | ||
} | ||
} | ||
printf("%lld\n", m % MOD); | ||
|
||
return 0; | ||
} | ||
|
||
//Correct Answer | ||
//Execution Time: 3.83 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
$t = <>; | ||
for (1 .. $t) { | ||
$g = <>; | ||
for (1 .. $g) { | ||
$_ = <>; | ||
($i, $n, $q) = split; | ||
++$n if $i != $q; | ||
print int($n / 2), "\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
const int MAXN = 12; | ||
|
||
long long ten[MAXN], fact[MAXN], perm[MAXN][MAXN], base[MAXN]; | ||
|
||
void init() { | ||
ten[0] = 1; | ||
fact[0] = 1; | ||
perm[0][0] = 1; | ||
for (int i = 1; i < MAXN; ++i) { | ||
ten[i] = ten[i - 1] * 10; | ||
fact[i] = fact[i - 1] * i; | ||
perm[i][0] = 1; | ||
for (int j = 1; j <= i; ++j) { | ||
perm[i][j] = perm[i][j - 1] * (i - j + 1); | ||
} | ||
} | ||
base[0] = base[1] = 0; | ||
for (int i = 2; i < MAXN; ++i) { | ||
base[i] = base[i - 1] + 9 * perm[9][i - 2]; | ||
} | ||
} | ||
|
||
int digit(long long n, int k) { | ||
return n / ten[k] % 10; | ||
} | ||
|
||
long long gao(long long n) { | ||
int m = 1; | ||
while (m < MAXN && n >= ten[m]) { | ||
++m; | ||
} | ||
if (m == MAXN) { | ||
return base[MAXN - 1]; | ||
} else { | ||
long long ret = base[m] + (digit(n, m - 1) - 1) * perm[9][m - 1]; | ||
int d = ((1 << 10) - 1) ^ (1 << digit(n, m - 1)); | ||
for (int i = m - 2; i >= 0; --i) { | ||
int t = digit(n, i); | ||
ret += __builtin_popcount(((1 << t) - 1) & d) * | ||
perm[__builtin_popcount(d) - 1][i]; | ||
if (d & (1 << t)) { | ||
d ^= 1 << t; | ||
} else { | ||
break; | ||
} | ||
} | ||
return ret; | ||
} | ||
} | ||
|
||
int main() { | ||
int re; | ||
long long l, r; | ||
|
||
init(); | ||
scanf("%d", &re); | ||
for (int ri = 1; ri <= re; ++ri) { | ||
scanf("%lld%lld", &l, &r); | ||
printf("%lld\n", gao(r + 1) - gao(l)); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
//Correct Answer | ||
//Execution Time: 0.75 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/tclsh | ||
|
||
set mod 1000000007 | ||
|
||
proc gcd {a b} { | ||
if {$b == 0} { | ||
return [list $a 1 0] | ||
} else { | ||
foreach {g x y} [gcd $b [expr {$a % $b}]] { | ||
return [list $g $y [expr {$x - $a / $b * $y}]] | ||
} | ||
} | ||
} | ||
|
||
for {set i 1} {$i <= 2500} {incr i} { | ||
foreach {g x y} [gcd $i $mod] { | ||
set inv($i) $x | ||
} | ||
} | ||
|
||
proc foo {n m} { | ||
global mod | ||
global inv | ||
set x 1 | ||
set y $x | ||
for {set i 1} {$n > 0 && $m > 0} {incr i; incr n -1; incr m -1} { | ||
set x [expr {$x * $n * $m * $inv($i) * $inv($i) % $mod}] | ||
incr y $x | ||
} | ||
return [expr {$y % $mod}] | ||
} | ||
|
||
proc bar {n m} { | ||
global mod | ||
global inv | ||
set x [expr {$n + 1}] | ||
set y $x | ||
for {set i 1; set j 2} {$n > 0 && $m > 0} {incr i; incr j; incr n -1; incr m -1} { | ||
set x [expr {$x * $n * $m * $inv($j) * $inv($i) % $mod}] | ||
incr y $x | ||
} | ||
return [expr {$y % $mod}] | ||
} | ||
|
||
set re [gets stdin] | ||
for {set ri 1} {$ri <= $re} {incr ri} { | ||
set s [gets stdin] | ||
set n [string length $s] | ||
set four [expr $n - [string length [string map {4 {}} $s]]] | ||
set seven [expr $n - [string length [string map {7 {}} $s]]] | ||
set ans 0 | ||
if {$seven == 0} { | ||
incr ans | ||
} else { | ||
incr ans [foo $four [expr $seven - 1]] | ||
if {$four >= 2} { | ||
incr ans [expr [bar [expr $four - 2] [expr $seven - 1]]] | ||
} | ||
} | ||
puts [expr $ans % $mod] | ||
} | ||
|
||
#Correct Answer | ||
#Execution Time: 0.77 | ||
|
Oops, something went wrong.