結果

問題 No.620 ぐるぐるぐるりん
ユーザー はむこはむこ
提出日時 2017-12-19 02:13:46
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 17,813 bytes
コンパイル時間 2,490 ms
コンパイル使用メモリ 209,256 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-12-17 20:11:34
合計ジャッジ時間 6,741 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void vizGraph(vvll&, int, std::string)’:
main.cpp:43:425: warning: ignoring return value of ‘int system(const char*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   43 | void vizGraph(vvll& g, int mode = 0, string filename = "out.png") { ofstream ofs("./out.dot"); ofs << "digraph graph_name {" << endl; set<P> memo; rep(i, g.size())  rep(j, g[i].size()) { if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i)))) continue; memo.insert(P(i, g[i][j])); ofs << "    " << i << " -> " << g[i][j] << (mode ? " [arrowhead = none]" : "")<< endl;  } ofs << "}" << endl; ofs.close(); system(((string)"dot -T png out.dot >" + filename).c_str()); }
      |                                                                                                                                                                                                                                                                                                                                                                                                                                   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

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

#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define repi(i,a,b) for(long long i = (long long)(a); i < (long long)(b); i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mt make_tuple
#define mp make_pair
#define ZERO(a) memset(a,0,sizeof(a))
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
#define exists find_if
#define forall all_of

using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using P = pair<ll, ll>;
using ld = long double;  using vld = vector<ld>; 
using vi = vector<int>; using vvi = vector<vi>; vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }

inline void input(int &v){ v=0;char c=0;int p=1; while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();} while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();} v*=p; }
template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) {  o << "(" << v.first << ", " << v.second << ")"; return o; }
template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
template<class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? "" : ", ") << get<Is>(t)), 0)...}; }
template<class Ch, class Tr, class... Args> 
auto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; }
ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; o << endl; } return o; }
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }
template <typename T>  ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T>  ostream &operator<<(ostream &o, const unordered_set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U>  ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]";  return o; }
template <typename T, typename U, typename V>  ostream &operator<<(ostream &o, const unordered_map<T, U, V> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]";  return o; }
vector<int> range(const int x, const int y) { vector<int> v(y - x + 1); iota(v.begin(), v.end(), x); return v; }
template <typename T> istream& operator>>(istream& i, vector<T>& o) { rep(j, o.size()) i >> o[j]; return i;}
string bits_to_string(ll input, ll n=64) { string s; rep(i, n) s += '0' + !!(input & (1ll << i)); reverse(all(s)); return s; }
template <typename T> ostream &operator<<(ostream &o, const priority_queue<T> &v) { auto tmp = v; while (tmp.size()) { auto x = tmp.top(); tmp.pop(); o << x << " ";} o << endl; return o; }

template <typename T> unordered_map<T, ll> counter(vector<T> vec){unordered_map<T, ll> ret; for (auto&& x : vec) ret[x]++; return ret;};
string substr(string s, P x) {return s.substr(x.fi, x.se - x.fi); }
void vizGraph(vvll& g, int mode = 0, string filename = "out.png") { ofstream ofs("./out.dot"); ofs << "digraph graph_name {" << endl; set<P> memo; rep(i, g.size())  rep(j, g[i].size()) { if (mode && (memo.count(P(i, g[i][j])) || memo.count(P(g[i][j], i)))) continue; memo.insert(P(i, g[i][j])); ofs << "    " << i << " -> " << g[i][j] << (mode ? " [arrowhead = none]" : "")<< endl;  } ofs << "}" << endl; ofs.close(); system(((string)"dot -T png out.dot >" + filename).c_str()); }

size_t random_seed; namespace std { using argument_type = P; template<> struct hash<argument_type> { size_t operator()(argument_type const& x) const { size_t seed = random_seed; seed ^= hash<ll>{}(x.fi); seed ^= (hash<ll>{}(x.se) << 1); return seed; } }; }; // hash for various class
namespace myhash{ const int Bsizes[]={3,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77,81}; const int xor_nums[]={0x100007d1,0x5ff049c9,0x14560859,0x07087fef,0x3e277d49,0x4dba1f17,0x709c5988,0x05904258,0x1aa71872,0x238819b3,0x7b002bb7,0x1cf91302,0x0012290a,0x1083576b,0x76473e49,0x3d86295b,0x20536814,0x08634f4d,0x115405e8,0x0e6359f2}; const int hash_key=xor_nums[rand()%20]; const int mod_key=xor_nums[rand()%20]; template <typename T> struct myhash{ std::size_t operator()(const T& val) const { return (hash<T>{}(val)%mod_key)^hash_key; } }; };
template <typename T> class uset:public std::unordered_set<T,myhash::myhash<T>> { using SET=std::unordered_set<T,myhash::myhash<T>>; public: uset():SET(){SET::rehash(myhash::Bsizes[rand()%20]);} };
uint32_t randxor() { static uint32_t x=1+(uint32_t)random_seed,y=362436069,z=521288629,w=88675123; uint32_t t; t=(x^(x<<11));x=y;y=z;z=w; return( w=(w^(w>>19))^(t^(t>>8)) ); }
struct timeval start; double sec() { struct timeval tv; gettimeofday(&tv, NULL); return (tv.tv_sec - start.tv_sec) + (tv.tv_usec - start.tv_usec) * 1e-6; }
struct init_{init_(){ gettimeofday(&start, NULL); ios::sync_with_stdio(false); cin.tie(0); struct timeval myTime; struct tm *time_st; gettimeofday(&myTime, NULL); time_st = localtime(&myTime.tv_sec); srand(myTime.tv_usec); random_seed = RAND_MAX / 2 + rand() / 2; }} init__;
#define rand randxor
#define ldout fixed << setprecision(40) 



typedef long double number;
const number eps = 1e-8;
using vec = vector<number>;
using mat = vector<vec>;

ostream &operator<<(ostream &o, const vec &v) { 
    for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << endl;
    return o; 
}
ostream &operator<<(ostream &o, const mat &v) { 
    for (int i = 0; i < v.size(); i++) { cout << v[i]; } 
    return o; 
}

// O( n^2 )
vec zero(int n) {
    return vec(n);
}
// O( n m )
mat zero(int n, int m) {
    mat A(n, vec(m, 0));
    return A;
}


// O( n^2 )
mat identity(int n) {
    mat A(n, vec(n, 0));
    for (int i = 0; i < n; ++i) A[i][i] = 1; // 積の単位元(和の単位元は?)
    return A;
}
// O( n^2 )
vec mul(const mat &A, const vec &x) {
    vec y(A.size(), 0);
    for (int i = 0; i < A.size(); ++i)
        for (int j = 0; j < A[0].size(); ++j)
            y[i] += A[i][j] * x[j]; // 加群の積と和の演算子
    return y;
}
// O( n^3 )
mat mul(const mat &A, const mat &B) {
    mat C(A.size(), vec(B[0].size(), 0));
    for (int i = 0; i < C.size(); ++i)
        for (int j = 0; j < C[i].size(); ++j)
            for (int k = 0; k < A[i].size(); ++k)
                C[i][j] += A[i][k] * B[k][j]; // 加群の積と和の演算子
    return C;
}
// O( n^3 log e )
mat pow(const mat &A, int e) {
    return e == 0 ? identity(A.size())  :
        e % 2 == 0 ? pow(mul(A, A), e/2) : mul(A, pow(A, e-1));
}
// O( n )
number inner_product(const vec &a, const vec &b) {
    number ans = 0;
    for (int i = 0; i < a.size(); ++i)
        ans += a[i] * b[i];
    return ans;
}
// O( n^3 )
number det(mat A) {
    const int n = A.size();
    number D = 1;
    for (int i = 0; i < n; ++i) {
        int pivot = i;
        for (int j = i+1; j < n; ++j)
            if (abs(A[j][i]) > abs(A[pivot][i])) pivot = j;
        swap(A[pivot], A[i]);
        D *= A[i][i] * (i != pivot ? -1 : 1);
        if (abs(A[i][i]) < eps) break;
        for(int j = i+1; j < n; ++j)
            for(int k = n-1; k >= i; --k)
                A[j][k] -= A[i][k] * A[j][i] / A[i][i];
    }
    return D;
}
// O(n)
number tr(const mat &A) {
    number ans = 0;
    for (int i = 0; i < A.size(); ++i)
        ans += A[i][i];
    return ans;
}
// O( n^3 ).
int rank(mat A) {
    const int n = A.size(), m = A[0].size();
    int r = 0;
    for (int i = 0; r < n && i < m; ++i) {
        int pivot = r;
        for (int j = r+1; j < n; ++j)
            if (abs(A[j][i]) > abs(A[pivot][i])) pivot = j;
        swap(A[pivot], A[r]);
        if (abs(A[r][i]) < eps) continue;
        for (int k = m-1; k >= i; --k)
            A[r][k] /= A[r][i];
        for(int j = r+1; j < n; ++j)
            for(int k = i; k < m; ++k)
                A[j][k] -= A[r][k] * A[j][i];
        ++r;
    }
    return r;
}

struct LUinfo {
    vector<number> value;
    vector<int> index;
};
// O( n^3 ), Gaussian forward elimination
LUinfo LU_decomposition(mat A) {
    const int n = A.size();
    LUinfo data;
    for (int i = 0; i < n; ++i) {
        int pivot = i;
        for (int j = i+1; j < n; ++j)
            if (abs(A[j][i]) > abs(A[pivot][i])) pivot = j;
        swap(A[pivot], A[i]);
        data.index.push_back(pivot);
        // if A[i][i] == 0, LU decomposition failed.
        for(int j = i+1; j < n; ++j) {
            A[j][i] /= A[i][i];
            for(int k = i+1; k < n; ++k)
                A[j][k] -= A[i][k] * A[j][i];
            data.value.push_back(A[j][i]);
        }
    }
    for(int i = n-1; i >= 0; --i) {
        for(int j = i+1; j < n; ++j)
            data.value.push_back(A[i][j]);
        data.value.push_back(A[i][i]);
    }
    return data;
}
// O( n^2 ) Gaussian backward substitution
vec LU_backsubstitution(const LUinfo &data, vec b) {
    const int n = b.size();
    int k = 0;
    for (int i = 0; i < n; ++i){
        swap(b[data.index[i]], b[i]);
        for(int j = i+1; j < n; ++j)
            b[j] -= b[i] * data.value[k++];
    }
    for (int i = n-1; i >= 0; --i) {
        for (int j = i+1; j < n; ++j)
            b[i] -= b[j] * data.value[k++];
        b[i] /= data.value[k++];
    }
    return b;
}

// reduce Hessenberg form (inplace). 
// O ( n^3 )
void hessenberg(mat &A) {
    const int n = A.size();
    for (int k = 1; k <= n-2; ++k) {
        vec u(n);
        for (int i = k; i < n; ++i) u[i] = A[i][k-1];

        number ss = 0;
        for (int i = k+1; i < n; ++i) ss += u[i] * u[i];
        if (abs(ss) <= 0.0) continue;
        number s = sqrt( ss + u[k]*u[k] );
        if (u[k] > 0.0) s = -s;

        u[k] -= s;
        number uu = sqrt( ss + u[k]*u[k] );
        for (int i = k; i < n; ++i) u[i] /= uu;

        vec f(n), g(n);
        for (int i = 0; i < n; ++i)
            for (int j = k; j < n; ++j)
                f[i] += A[i][j] * u[j],
                    g[i] += A[j][i] * u[j];
        number gamma = inner_product(u, g);
        for (int i = 0; i < n; ++i)
            f[i] -= gamma * u[i],
                g[i] -= gamma * u[i];

        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                A[i][j] = A[i][j] - 2*u[i]*g[j] - 2*f[i]*u[j];
    }
}

// find all eigenvalues using Hessenberg-QR Method
// O( n^3 + M n^2 ) where M is the number of iterations.
vector<number> eigenvalues(mat A) {
    const int n = A.size();
    hessenberg(A);
    vector<number> s(n), c(n);
    for (int m = n; m >= 2; ) {
        if (abs(A[m-1][m-2]) < eps) { --m; continue; }
        number shift = A[m-1][m-1];
        for (int i = 0; i < m; ++i) A[i][i] -= shift;
        for (int k = 0; k < m-1; ++k) {
            number a = A[k][k], b = A[k+1][k], r = sqrt(a*a + b*b);
            s[k] = r == 0.0 ? 0.0 : b/r,
                c[k] = r == 0.0 ? 0.0 : a/r;
            for (int j = k; j < m; ++j) {
                number x = A[k][j], y = A[k+1][j];
                A[ k ][j] =  c[k] * x + s[k] * y;
                A[k+1][j] = -s[k] * x + c[k] * y;
            }
        }
        for (int k = 0; k < m-1; ++k) {
            for (int i = 0; i <= k+1; ++i) {
                number x = A[i][k], y = A[i][k+1];
                A[i][ k ] =  c[k] * x + s[k] * y;
                A[i][k+1] = -s[k] * x + c[k] * y;
            }
        }
        for (int i = 0; i < m; ++i) A[i][i] += shift;
    }
    vector<number> lambda;
    for (int i = 0; i < n; ++i)
        lambda.push_back( A[i][i] );
    return lambda;
}

// find the corresponding eigenvector from the eigenvalue.
// O ( n^3 + M n^2 ) where M is the number of iterations.
vec eigenvector(mat A, number lambda) {
    const int n = A.size();
    vec y(n); y[0] = 1;
    for (int i = 0; i < n; ++i) A[i][i] -= lambda;
    LUinfo data = LU_decomposition(A);
    number mu, v2, v2s;
    do {
        vec v = LU_backsubstitution(data, y); // A v = y 
        mu = inner_product(v, y);
        v2 = inner_product(v, v);
        v2s = sqrt(v2);
        for (int j = 0; j < n; ++j) y[j] = v[j] / v2s;
    } while (abs(1.0-mu*mu/v2) > eps);
    return y;
}

mat operator*(const mat& t1, const mat& t2) {
    assert(t1[0].size() == t2.size());
    mat ret = zero(t1.size(), t2[0].size()); 
    rep(i, t1.size()) {
        rep(j, t2[0].size()) {
            rep(k, t1[0].size()) {
                ret[i][j] += t1[i][k] * t2[k][j];
            }
        }
    }
    return ret;
}
vec operator*(const mat& t1, const vec& t2) {
    assert(t1[0].size() == t2.size());
    vec ret(t1.size());
    rep(i, t1.size()) {
        rep(j, t2.size()) {
            ret[i] += t1[i][j] * t2[j];
        }
    }
    return ret;
}
vec operator+(const vec& t1, const vec& t2) {
    assert(t1.size() == t2.size());
    vec ret(t1.size());
    rep(i, t1.size()) {
        ret[i] = t1[i] + t2[i];
    }
    return ret;
}
mat operator+(const mat& t1, const mat& t2) {
    assert(t1.size() == t2.size() && t1[0].size() == t2[0].size());
    mat ret = zero(t1.size(), t1[0].size()); 
    rep(i, t1.size()) {
        rep(j, t1[0].size()) {
            ret[i][j] = t1[i][j] + t2[i][j];
        }
    }
    return ret;
}
vec operator-(const vec& t1, const vec& t2) {
    assert(t1.size() == t2.size());
    vec ret(t1.size());
    rep(i, t1.size()) {
        ret[i] = t1[i] - t2[i];
    }
    return ret;
}
mat operator-(const mat& t1, const mat& t2) {
    assert(t1.size() == t2.size() && t1[0].size() == t2[0].size());
    mat ret = zero(t1.size(), t1[0].size()); 
    rep(i, t1.size()) {
        rep(j, t1[0].size()) {
            ret[i][j] = t1[i][j] - t2[i][j];
        }
    }
    return ret;
}
mat operator-(const mat& t1) {
    mat ret = t1;
    rep(i, ret.size()) {
        rep(j, ret[0].size()) {
            ret[i][j] *= -1;
        }
    }
    return ret;
}

mat inverse(const mat& t) {
    assert(t.size() == t[0].size());
    assert(t.size() == 2); // TODO
    mat ret = zero(2, 2);
    ret[0][0] = t[1][1];
    ret[0][1] = -t[1][0];
    ret[1][0] = -t[0][1];
    ret[1][1] = t[0][0];
    rep(i, 2) rep(j, 2) {
        ret[i][j] /= (t[0][0] * t[1][1] - t[0][1] * t[1][0]);
    }

    return ret;
}
mat transpose(const mat& t) {
    mat ret = zero(t[0].size(), t.size());
    rep(i, t.size()) rep(j, t[0].size()) {
        ret[j][i] = t[i][j];
    }
    return ret;
}
mat block(const mat& t, ll si, ll sj, ll i0, ll j0) {
    assert(i0+si<=t.size() && j0+sj <= t[0].size());
    mat ret = zero(si, sj);
    rep(i, si) rep(j, sj) {
        ret[i][j] = t[i0+i][j0+j];
    }
    return ret;
}


number omega = 0.01;
number v = 0.0001;
vec f(vec x, vec u) {
    mat F_x = zero(2, 2);
    F_x[0][0] = v+1, F_x[0][1] = -omega;
    F_x[1][0] = omega, F_x[1][1] = v+1;
    return F_x * x + u;
}

// Sx(S+U)
mat getF(void) {
    mat F = zero(2, 4);
    F[0][0] = v+1; F[0][1] = -omega; F[0][2] = 1; F[0][3] = 0;
    F[1][0] = omega; F[1][1] = v+1; F[1][2] = 0; F[1][3] = 1;
    return F;
}
// Sx1
vec getf(void) {
    return zero(2);
}

pair<number, number> evaluate(number g_x, number g_y, number output_coeff, ll T) {
    // X=U=2
    vector<mat> C(T+1, zero(4, 4));
    vector<vec> c(T+1, zero(4));
    C[T][0][0] = 1, C[T][1][1] = 1;
    c[T][0] = -g_x, c[T][1] = -g_y;
    rep(t, T+1) C[t][2][2] = output_coeff, C[t][3][3] = output_coeff;

    vector<mat> Q(T+1, zero(4, 4)); // (S+U)x(S+U)
    vector<vec> q(T+1, zero(4)); // (S+U)x1
    vector<mat> V(T+2, zero(2, 2)); // SxS
    vector<vec> v(T+2, zero(2)); // Sx1
    vector<mat> K(T+2, zero(2, 2)); // UxS
    vector<vec> k(T+2, zero(2)); // Ux1

    for (int t = T; t >= 0; t--) {
        mat F = getF(); // Sx(S+U)
        vec f = getf(); // Sx1
        Q[t] = C[t] + transpose(F) * V[t+1] * F;
        q[t] = c[t] + transpose(F) * V[t+1] * f + transpose(F) * v[t+1];
        mat Qxx = block(Q[t], 2, 2, 0, 0);
        mat Quu = block(Q[t], 2, 2, 2, 2);
        mat Qxu = block(Q[t], 2, 2, 0, 2);
        mat Qux = block(Q[t], 2, 2, 2, 0);
        vec qx = zero(2); qx[0] = q[t][0], qx[1] = q[t][1];
        vec qu = zero(2); qu[0] = q[t][2], qu[1] = q[t][3];
        K[t] = -inverse(Quu) * Qux;
        k[t] = -inverse(Quu) * qu;
        V[t] = Qxx + Qxu * K[t] + transpose(K[t]) * Qux + transpose(K[t]) * Quu * K[t];
        v[t] = qx + Qxu * k[t] + transpose(K[t]) * qu + transpose(K[t]) * Quu * k[t];
    }
    vector<vec> x(T+2, zero(2));
    vector<vec> u(T+2, zero(2));
    x[0][0] = 1, x[0][1] = 0;
    rep(t, T) {
        vec u_tmp = K[t] * x[t] + k[t];
        u[t] = u_tmp;
        x[t+1] = f(x[t], u[t]);
    }
    
//    rep(i, T+1) cout << " " << x[i][0] << " " << x[i][1] << endl;
    rep(i, T) cout << u[i][0] << " " << u[i][1] << endl;

    number usum = 0;
    rep(i, T) usum += u[i][0] * u[i][0] + u[i][1] * u[i][1];
    return mp(usum, sqrt(pow(x[T][0] - g_x, 2) + pow(x[T][1] - g_y, 2)));
}

// omega = 1, v = 0.1
// omega = 1, v = 1
int main(int argc, char** argv) {
    cout << ldout;
    number g_x, g_y, output_coeff = 1e-9;
    number k;
    ll T;
    cin >> T >> k >> omega >> v >> g_x >> g_y;
    auto err = evaluate(g_x, g_y, output_coeff, T);
//    cerr << err << endl;

    return 0;
}
0