結果

問題 No.695 square1001 and Permutation 4
ユーザー ojisan_ITojisan_IT
提出日時 2018-06-14 22:05:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 900 ms / 7,000 ms
コード長 2,688 bytes
コンパイル時間 1,840 ms
コンパイル使用メモリ 168,604 KB
実行使用メモリ 42,220 KB
最終ジャッジ日時 2023-09-30 01:37:17
合計ジャッジ時間 7,532 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 44 ms
5,316 KB
testcase_02 AC 79 ms
13,288 KB
testcase_03 AC 48 ms
13,208 KB
testcase_04 AC 207 ms
13,104 KB
testcase_05 AC 268 ms
13,112 KB
testcase_06 AC 532 ms
23,048 KB
testcase_07 AC 409 ms
23,048 KB
testcase_08 AC 391 ms
22,872 KB
testcase_09 AC 536 ms
22,872 KB
testcase_10 AC 110 ms
7,304 KB
testcase_11 AC 780 ms
42,192 KB
testcase_12 AC 765 ms
42,220 KB
testcase_13 AC 900 ms
42,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vt = vector<T>;
template<class T> using vvt = vector<vt<T>>;
template<class T> using ttt = tuple<T,T>;
using tii = tuple<int,int>;
using vi = vector<int>;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(),(a).end()
#define FST first
#define SEC second
#define DEB cerr<<"!"<<endl
#define SHOW(a,b) cerr<<(a)<<" "<<(b)<<endl
#define DIV int(1e9+7)
const int INF = (INT_MAX/2);
const ll LLINF = (LLONG_MAX/2);
const double eps = 1e-8;
//const double PI = M_PI;  
inline ll pow(ll x,ll n,ll m){ll r=1;while(n>0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;}
inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;}

/*Coding Space*/
// 次の等式が非常に直観的で便利
// x = sum(c_i*d_i*u_i) mod P
// [x : ans], [c_i : P/p_i], [d_i : p_iを法とするc_iの逆元], [u_i : p_iのときの余り], [P : 法p_iの積]  (d_i*c_i == 1 (mod p_i))
// フェルマーの小定理を利用しているので互いに素である必要がある

// <ll>で宣言しておいて、a, a_div, b, b_divは全部int型に収めておいた方がいい。あるいは多倍長整数型を使おう
// https://yukicoder.me/wiki/%E4%B8%AD%E8%8F%AF%E9%A2%A8%E5%89%B0%E4%BD%99%E5%AE%9A%E7%90%86
// pair数が小さかったり、オンラインクエリだったらこれ⇔Lagrange CRA
template <class T = long long> // !! div == 0はやめろ
T Newton_crt(T a, T a_div, T b, T b_div){ // 互いに素である必要?
  T t = pow(a_div, b_div - 2, b_div);
  t *= (b - a) + b_div;
  t %= b_div;
  return (a + a_div*t)%(a_div*b_div);
}
int main(){
  int n,m; cin >> n >> m;
  vi in(m); rep(i,m) cin >> in[i];
  ll div1 = 17 * 9920467;
  ll div2 =    592951213;
  ll ans_div1 = 0;
  int d = (n&1?1:0);
  {
    vi dp(n/2+d+1,0);
    rep(i,n/2+d+1){
      rep(j,m){
        if(i - in[j] >= 0) dp[i] += dp[i - in[j]] % div1;
        else dp[0] = 1;
        dp[i] %= div1;
      }
    }
    rep(i,n/2+d+1){
      rep(j,m){
        if(i + in[j] > n/2 + d&& i + in[j] < n)  ans_div1 += ((ll)dp[i] * dp[n-i-in[j]-1])%div1;
        ans_div1 %= div1;
      }
    }
  }
  
  ll ans_div2 = 0;
  {
    vi dp(n/2+d+1);
    rep(i,n/2+d+1){
      rep(j,m){
        if(i - in[j] >= 0) dp[i] += dp[i - in[j]] % div2;
        else dp[0] = 1;
        dp[i] %= div2;
      }
    }
    rep(i,n/2+d+1){
      rep(j,m){
        if(i + in[j] > n/2 + d&& i + in[j] < n)  ans_div2 += ((ll)dp[i] * dp[n-i-in[j]-1])%div2;
        ans_div2 %= div2;
      }
    }
  }
  
  //SHOW(ans_div1, ans_div2);
  cout << Newton_crt(ans_div1, div1, ans_div2, div2) << endl;
}
0