結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー tarattata1tarattata1
提出日時 2020-02-14 23:01:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,525 ms / 2,000 ms
コード長 3,838 bytes
コンパイル時間 1,192 ms
コンパイル使用メモリ 101,616 KB
実行使用メモリ 15,948 KB
最終ジャッジ日時 2023-08-10 02:25:56
合計ジャッジ時間 5,886 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 44 ms
6,800 KB
testcase_11 AC 18 ms
4,376 KB
testcase_12 AC 1,522 ms
5,348 KB
testcase_13 AC 18 ms
4,376 KB
testcase_14 AC 32 ms
4,376 KB
testcase_15 AC 18 ms
4,376 KB
testcase_16 AC 55 ms
7,640 KB
testcase_17 AC 15 ms
4,376 KB
testcase_18 AC 1,525 ms
5,352 KB
testcase_19 AC 65 ms
4,376 KB
testcase_20 AC 185 ms
15,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
ll MOD;
//const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;


vector<int> pp;
void preparePrime( int maxp )
{
    int i;
    vector<int> a(maxp+1);
    for(i=2; i<=maxp; i++) {
        int k=i+i;
        while(k<=maxp) {
            a[k]=1;
            k+=i;
        }
    }
    for(i=2; i<=maxp; i++) {
        if(a[i]==0) pp.push_back( i );
    }
    return;
}

void decompositPrime( ll a, vector<pair<ll,ll> >& vv )
{
    preparePrime( (int)sqrt((double)a)+1 );

    int i;
    for(i=0; i<(int)pp.size(); i++) {
        int count = 0;
        while(a % pp[i] == 0 ) {
            a /= pp[i];
            count ++;
        }
        if(count>0) vv.push_back( make_pair( pp[i], count ) );
        if (a==1) break;
    }
    if(a>1) {
        vv.push_back( make_pair( a, 1 ) );
    }

    return;
}

char str[5];
void solve()
{
    int n,m,K;
    scanf("%d%d%d", &n, &m, &K);
    scanf("%s", str);
    int flag=(str[0]=='+'? 0: 1);
    vector<int> a(n),b(m);
    int i,j;
    for(i=0; i<m; i++) {
        scanf("%d", &b[i]);
    }
    for(i=0; i<n; i++) {
        scanf("%d", &a[i]);
    }

    if(flag==0) {
        map<int,pair<int,int> > z;
        for(i=0; i<n; i++) {
            z[a[i]%K].first++;
        }
        for(i=0; i<m; i++) {
            z[(K-(b[i]%K))%K].second++;
        }
        ll ans=0;
        auto it=z.begin();
        for(; it!=z.end(); ++it) {
            int a=it->second.first, b=it->second.second;
            ans+=(ll)a*b;
        }
        printf("%lld\n", ans);
    }
    else {
        vector<pair<ll,ll> > vv;
        decompositPrime( K, vv );

        int siz=(int)vv.size();
        int k;
        map<vector<int>, int> mp0, mp1;
        for(i=0; i<n; i++) {
            vector<int> now;
            for(k=0; k<siz; k++) {
                ll p=vv[k].first;
                ll q=vv[k].second;
                ll curr=a[i];
                int cnt=0;
                while(curr>0 && curr%p==0 && cnt<=q) {
                    cnt++;
                    curr/=p;
                }
                now.push_back(cnt);
            }
            mp0[now]++;
        }

        for(i=0; i<m; i++) {
            vector<int> now;
            for(k=0; k<siz; k++) {
                ll p=vv[k].first;
                ll q=vv[k].second;
                ll curr=b[i];
                int cnt=0;
                while(curr>0 && curr%p==0 && cnt<=q) {
                    cnt++;
                    curr/=p;
                }
                now.push_back(cnt);
            }
            mp1[now]++;
        }

        ll ans=0;
        auto it0=mp0.begin();
        for(; it0!=mp0.end(); ++it0) {
            auto it1=mp1.begin();
            for(; it1!=mp1.end(); ++it1) {
                const vector<int>& v0=it0->first;
                const vector<int>& v1=it1->first;
                int siz0=(int)v0.size(), siz1=(int)v1.size();
                assert(siz0==siz && siz1==siz);
                for(k=0; k<siz; k++) {
                    if(v0[k]+v1[k]<vv[k].second) break;
                }
                if(k==siz) {
                    ans+=(ll)(it0->second) * (it1->second);
                }
            }
        }
        printf("%lld\n", ans);
    }

    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T; scanf("%d", &T);
    while(T--) {
        solve();
    }
#endif
    return 0;
}
0