robert-king / nzpc-2018 Goto Github PK
View Code? Open in Web Editor NEWnew zealand programming contest 2018
new zealand programming contest 2018
#include "bits/stdc++.h"
//#include "ext/pb_ds/tree_policy.hpp"
//#include "ext/pb_ds/assoc_container.hpp"
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define iout(x) printf("%d\n",x)
#define lout(x) printf("%lld\n",x)
#define REP(x,l,u) for(ll x = l;x<u;x++)
#define RREP(x,l,u) for(ll x = l;x>=u;x--)
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) a.begin(),a.end()
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define sqr(x) ((x)*(x))
#define lowbit(x) (x&(-x))
#define lson (ind<<1)
#define rson (ind<<1|1)
#define se second
#define fi first
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl;
#define sz(x) ((int)x.size())
#define EX0 exit(0);
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;
typedef vector<ll> VLL;
typedef vector<int> VI;
const int block_size = 320;
typedef complex<ll> point;
const ll mod = 1e9+7;
const ll inf = 1e9+7;
const ld eps = 1e-9;
const db PI = atan(1)*4;
template<typename T>
inline int sign(const T&a) {
if(a<0)return -1;
if(a>0)return 1;
return 0;
}
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename T> inline void in(T &x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
x *= f;
}
ll twop(int x) {
return 1LL<<x;
}
template<typename A,typename B > inline void in(A&x,B&y) {
in(x);
in(y);
}
template<typename A,typename B,typename C>inline void in(A&x,B&y,C&z) {
in(x);
in(y);
in(z);
}
template<typename A,typename B,typename C,typename D> inline void in(A&x,B&y,C&z,D&d) {
in(x);
in(y);
in(z);
in(d);
}
const ll inv = 166666668;
// m must be positive
template<typename T>
static T MOD(T a, T m)
{
a %= m;
if (a < 0)
a += m;
return a;
}
// a must be relatively prime to m
template<typename T>
static T inverse(T a, T m)
{
a = MOD(a, m);
if (a <= 1)
return a;
return MOD((1 - inverse(m, a) * m) / a, m);
}
vector<VLL>equ;
set<int>reachable;
namespace SOLVE {
int n;
vector<int>v(1000);
void bfs(){
int cur = 1;
queue<int>q;
q.push(cur);
while (sz(q)) {
int f = q.front();q.pop();
reachable.insert(f);
REP(i,1,7){
if(f+i <= n){
if(v[f+i] == 0 && reachable.count(f+i)==0)q.push(f+i);
else{if(reachable.count(v[f+i])==0) q.push(v[f+i]);}
}
}
}
reachable.insert(n);
}
void main(){
cin>>n;
REP(i,1,n+1)in(v[i]);
bfs();
int row = 0;
// for(auto i:reachable)dbg(i);
REP(i,1,n+1){
if(v[i] == 0 && reachable.count(i)){
VLL tmp(n+1,0);
tmp[i-1] = 1;
tmp[n] = 1;
REP(j,1,7){
if(i+j < n){
if(v[i+j] == 0){
(tmp[i+j-1] += mod-inv)%=mod;
}else{
(tmp[v[i+j]-1] += mod-inv)%=mod;
}
}
}
equ.PB(tmp);
}
}
equ.back().back() = 0;
// REP(i,0,sz(equ)){
// REP(j,0,n+1){
// cout<<equ[i][j]<<" ";
// }
// cout<<endl;
// }
REP(i,0,sz(equ))swap(equ[i][0],equ[i][n-1]);
int curRow = 0;
REP(i,0,n){
bool exist = 0;
REP(j,curRow,sz(equ)){
if(equ[j][i]){
swap(equ[j], equ[curRow]);
exist = 1;
break;
}
}
if(exist){
// dbg(i);
REP(c,i+1,n+1){
equ[curRow][c] *= inverse(equ[curRow][i], mod);
(equ[curRow][c])%=mod;
}
equ[curRow][i] = 1;
REP(r,curRow+1,sz(equ)){
REP(c,i+1,n+1){
equ[r][c] -= equ[curRow][c] * equ[r][i];
equ[r][c] = ((equ[r][c])%mod+mod)%mod;
}
equ[r][i] = 0;
}
curRow++;
}
}
// REP(i,0,sz(equ)){
// REP(j,0,n+1){
// cout<<equ[i][j]<<" ";
// }
// cout<<endl;
// }
cout<<equ.back()[n]<<endl;
}
}
int main() {
SOLVE::main();
return 0;
}
BTW, could you plz briefly explain the idea for K? I tried to go backwards but it ended up with TLE. Thanks a lot.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.