結果

問題 No.620 ぐるぐるぐるりん
ユーザー はむこはむこ
提出日時 2017-12-19 03:57:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 89 ms / 1,000 ms
コード長 13,792 bytes
コンパイル時間 2,105 ms
コンパイル使用メモリ 189,768 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 21:22:36
合計ジャッジ時間 4,175 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 89 ms
4,384 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 88 ms
4,380 KB
testcase_29 AC 88 ms
4,380 KB
testcase_30 AC 89 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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 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; long 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 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;
}

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.5;
number vv = -0.5;
vec f(vec x, vec u) {
    mat F_x = zero(2, 2);
    F_x[0][0] = vv+(number)1, F_x[0][1] = -omega;
    F_x[1][0] = omega, F_x[1][1] = vv+(number)1;
    return F_x * x + u;
}

// Sx(S+U)
mat getF(void) {
    mat F = zero(2, 4);
    F[0][0] = vv+(number)1; F[0][1] = -omega; F[0][2] = (number)1; F[0][3] = 0;
    F[1][0] = omega; F[1][1] = vv+(number)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
//    cout <<"HOGE"<<endl;

    for (int t = T; t >= 0; t--) {
//    cout <<t<<" "<<"HOGE"<<endl;
        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<number> x_brutal(T+2);
    vector<number> y_brutal(T+2);
    vector<vec> u(T+2, zero(2));
    x[0][0] = 1, x[0][1] = 0;
    x_brutal[0] = 1;
    rep(t, T) {
        vec u_tmp = K[t] * x[t] + k[t];
        u[t] = u_tmp;
        x[t+1] = f(x[t], u[t]);

        number ux = u[t][0];
        number uy = u[t][1];
        x_brutal[t+1] = x_brutal[t] - y_brutal[t] * omega + x_brutal[t] * vv + ux;
        y_brutal[t+1] = y_brutal[t] + x_brutal[t] * omega + y_brutal[t] * vv + uy;
    }
    
    /*
    rep(i, T+1) cout << " " << x[i][0] << " " << x[i][1] << endl;
    */
    rep(i, T) cout << ldout << 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_brutal[T] - g_x, 2) + pow(y_brutal[T] - g_y, 2)));
}

ll randr(ll i, ll j) { if (i > j) return i; else return (ll)rand() % (j - i + 1) + i; }
number randrf(number i, number j) { 
    number ret = ((number)rand() / (number)RAND_MAX) * ((number)j - (number)i) + (number)i; 
    return ret;
}
int main(int argc, char** argv) {
    cout << ldout;
    number g_x, g_y, output_coeff = 1e-9;
    number k;
    ll T;
#if 0
    /*
    {
        T = 1;
        k = 1e9;
        omega = randrf(-10, 10);
        vv = randrf(-10, 10);
        g_x = randrf(-100, 100);
        g_y = randrf(-100, 100);
        auto err = evaluate(g_x, g_y, output_coeff, T);
        assert(err.se <= 1e-4);
        assert(err.fi <= k);
        cout << ldout << T << " " << err.fi * 1.01 << endl;
        cout << omega << " " << vv << endl;
        cout << g_x << " " << g_y << endl;
    }
    */
    {
        ll q = 1000;
        cout << q << endl;
        rep(_, q) {
            T = 15;
            k = 1e9;
            omega = randrf(-10, 10);
            vv = randrf(-10, 10);
            g_x = randrf(-100, 100);
            g_y = randrf(-100, 100);
            auto err = evaluate(g_x, g_y, output_coeff, T);
            assert(err.se <= 1e-4);
            assert(err.fi <= k);
            cout << ldout << T << " " << 1e9 << " " << omega << " " << vv << " " << g_x << " " << g_y << endl;
        }
    }
    /*
    rep(_, 100000) {
        T = randr(15, 15);
        k = _ * 1;
        omega = 1;
        vv = -1;
        g_x = 91;
        g_y = -48;
        auto err = evaluate(g_x, g_y, output_coeff, T);
        if (err.se && err.fi <= k) {
            cout << ldout << T << " " << err.fi * 1.01 << endl;
            cout << omega << " " << vv << endl;
            cout << g_x << " " << g_y << endl;
            exit(0);
        }
    }
    */
#else
    ll q; cin >> q;
    rep(_, q) {
        cin >> T >> k >> omega >> vv >> g_x>> g_y;
        assert(1<=T&&T<=15);
        assert(0<=k&&k<=1e9);
        assert(-10<=omega&&omega<=10);
        assert(-10<=vv&&vv<=10);
        assert(-100<=g_x&&g_x<=100);
        assert(-100<=g_y&&g_y<=100);
        auto err = evaluate(g_x, g_y, output_coeff, T);
//        cerr << err << endl;
        //    cout << err.fi << " " << err.se << " " << T << " " << k << " " << vv << " " << g_x << " " << g_y << endl;
        assert(err.se <= 1e-4);
    }
#endif

    return 0;
}
0