結果

問題 No.695 square1001 and Permutation 4
ユーザー chocoruskchocorusk
提出日時 2018-12-19 11:34:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,783 ms / 7,000 ms
コード長 1,600 bytes
コンパイル時間 737 ms
コンパイル使用メモリ 86,288 KB
実行使用メモリ 42,456 KB
最終ジャッジ日時 2023-09-30 01:38:51
合計ジャッジ時間 12,619 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 84 ms
7,448 KB
testcase_02 AC 191 ms
23,824 KB
testcase_03 AC 78 ms
23,760 KB
testcase_04 AC 461 ms
23,968 KB
testcase_05 AC 771 ms
23,828 KB
testcase_06 AC 1,783 ms
42,424 KB
testcase_07 AC 829 ms
42,456 KB
testcase_08 AC 507 ms
42,424 KB
testcase_09 AC 1,262 ms
42,456 KB
testcase_10 AC 359 ms
11,616 KB
testcase_11 AC 1,701 ms
42,356 KB
testcase_12 AC 1,224 ms
42,352 KB
testcase_13 AC 1,679 ms
42,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const int mid=10000000;
int MOD[3]={17, 9920467, 592951213};
int dp[mid];
vector<int> x1, x2;
int n, m;
ll solve(int mod){
  fill(dp, dp+min(mid, n), 0);
  dp[0]=1;
  for(int i=1; i<min(mid, n); i++){
    for(auto x:x1){
      if(i-x>=0) dp[i]=(dp[i]+dp[i-x])%mod;
    }
  }
  if(n<=mid) return (ll)dp[n-1];
  ll ans=0;
  for(auto x:x2){
    for(int i=0; i<n-x; i++){
      ll c1=dp[i], c2=dp[n-1-i-x];
      ans+=(c1*c2);
      ans%=mod;
    }
  }
  for(int i=0; i<n-mid; i++){
    int c=0;
    for(auto x:x1){
      c+=dp[(i+mid-x)%mid];
      c%=mod;
    }
    dp[i]=c;
  }
  ans+=dp[n-1-mid];
  ans%=mod;
  return ans;
}
ll extgcd(ll a, ll b, ll& x, ll& y){
	ll d=a;
	if(b!=0){
		d=extgcd(b, a%b, y, x);
		y-=(a/b)*x;
	}else{
		x=1, y=0;
	}
	return d;
}
ll inv(ll a, ll p){
	ll x, y;
	extgcd(a, p, x, y);
	if(x<0){
		return p-(-x%p);
	}else{
		return x%p;
	}
}
int main()
{
	cin>>n>>m;
  for(int i=0; i<m; i++){
    int x;cin>>x;
    if(x<mid) x1.push_back(x);
    else x2.push_back(x);
  }
  ll ans[3];
  for(int i=0; i<3; i++) ans[i]=solve(MOD[i]);
  ll a[3]; a[0]=ans[0];
  a[1]=(ans[1]+MOD[1]-a[0])*inv(MOD[0], MOD[1])%MOD[1];
  a[2]=(ans[2]-(a[1]*MOD[0]+a[0])%MOD[2]+MOD[2])*inv(MOD[0]*MOD[1]%MOD[2], MOD[2])%MOD[2];
  cout<<a[0]+a[1]*MOD[0]+a[2]*MOD[0]*MOD[1]<<endl;
	return 0;
}
0