首页 > 其他分享 >[atcoder 349] [F - Subsequence LCM]

[atcoder 349] [F - Subsequence LCM]

时间:2024-04-27 20:11:51浏览次数:14  
标签:atcoder java int long Subsequence static import LCM public

SOS DP 学习笔记
Link here:

代码:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;


public class Main {
    static int n;
    static long m;
    static long[] a;

    public static void readData() throws IOException {
        // init data
        n = rd.nextInt();
        m = rd.nextLong();
        a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = rd.nextLong();
        }
    }

    static List<Long> list;

    public static void fenjie() {
        list = new ArrayList<>();
        long num = m;
        for (long i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                long p = 1;
                while (num % i == 0) {
                    num = num / i;
                    p *= i;
                }
                list.add(p);
            }
        }
        if (num > 1) {
            list.add(num);
        }
    }

    static int[] msk;
    static int size;
    static int[] cnt;

    static int sum;

    public static void mask() {
        size = list.size();
        sum = 0;
        msk = new int[n];
        Arrays.fill(msk, -1);
        cnt = new int[1 << size];
        for (int i = 0; i < n; i++) {
            if (m % a[i] == 0) {
                sum++;
                int mask = 0;
                for (int j = 0; j < list.size(); j++) {
                    if (a[i] % list.get(j) == 0) {
                        mask |= (1 << j);
                    }
                }
                cnt[mask]++;
                msk[i] = mask;
            }
        }
    }

    static long[] p;

    public static void init2() {
        p = new long[n + 1];
        p[0] = 1;
        for (int i = 1; i <= n; i++) {
            p[i] = p[i - 1] * 2 % 998244353;
        }
    }

    public static void main(String[] args) throws IOException {
        readData();
        fenjie();
        mask();
        init2();
        // handle special value 1
        if (m == 1) {
            System.out.println(p[sum] - 1);
            return;
        }
        // f[i][j]  前i个状态中选出或位j的方案数.
        long[] f = new long[1 << size];

        // 前0个状态选出位为mask的方案数
        f[0] = p[cnt[0]];

        //
        for (int i = 1; i < (1 << size); i++) {
            long[] next = new long[1 << size];
            for (int j = 0; j < (1 << size); j++) {
                next[j] = f[j];
            }
            for (int j = 0; j < (1 << size); j++) {
                if (cnt[i] > 0) {
                    //当前状态可选
                    next[i | j] += f[j] * (p[cnt[i]] - 1) % 998244353;
                    next[i | j] %= 998244353;
                }
            }
            f = next;
        }

        System.out.println(f[(1 << size) - 1]);
    }

}

class rd {
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    // nextLine()读取字符串
    static String nextLine() throws IOException {
        return reader.readLine();
    }

    // next()读取字符串
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    // 读取一个int型数值
    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    // 读取一个double型数值
    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    // 读取一个long型数值
    static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    // 读取一个BigInteger
    static BigInteger nextBigInteger() throws IOException {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

标签:atcoder,java,int,long,Subsequence,static,import,LCM,public
From: https://www.cnblogs.com/fishcanfly/p/18162443

相关文章

  • AtCoder Beginner Contest 350 A - G 题解
    AtCoderBeginnerContest350A-PastABCsSolution把最后三个字符转成数字判断即可Code#include<bits/stdc++.h>usingnamespacestd;intmain(){strings;cin>>s;s=s.substr(3,3);intx=0;x=(s[0]-'0')*100+(s[1]-�......
  • atcoder regular 176 (ARC176) A、B题解
     A很容易有一个错误想法,就是行从1~n,列从1~n拿,这样,第三个样例,最后,第7行,第7列,需要都增加两个数,但是(7,7)这个位置只能有一个数。我的做法是优先队列/set队列,每次选择行、列之中当前已经有的数目最少的(这样它们最需要添加),这样能保证,行列需要添加的,不会出现只能选择多个行列一样的......
  • AtCoder Beginner Contest 350 G - Mediator
    链接:https://atcoder.jp/contests/abc350/tasks/abc350_g大致题意:给出n个点,q个询问1号询问要求u,v之前加一条无向边图始终是一个森林2号询问询问是否有一个点与u,v都相邻,若有则输出该点,若无则输出0。询问强制在线。思路:在题目要求的图中,满足2号询问的点只有三种情况:要么这个......
  • AtCoder Beginner Contest 350
    B-DentistAoki难度:⭐题目大意现在有数列1~n,现在有m次操作,每次给出一个x,如果x存在就是删去,不存在就加上;问最后数列还剩多少个;解题思路数据很小,暴力就行;神秘代码#include<bits/stdc++.h>#defineintlonglong#defineIOSios::sync_with_stdio......
  • Atcoder ABC 350 全题解
    题外话别以为博主之前几场ABC都咕咕咕了,最近状态不好,这次ABC终于回来了(也有可能是题目变容易了,有图为证)P.S.请耐心看到最后!!否则后果自负!!!AB这年头谁不会AB啊当然有。不学OI的人。C考虑选择排序,依次将$1,2,3\cdots$与它们应该在的位置进行交换。那如果真的......
  • AtCoder Beginner Contest 350
    A-PastABCs(abc350A)题目大意给定一个形如ABCXXX的字符串。问XXX是否是\(001\to349\)之间,且不能是\(316\)。解题思路将后三位转换成数字后判断即可。神奇的代码a=int(input().strip()[3:])ifa>=1anda<=349anda!=316:print("Yes")else:p......
  • AtCoder Beginner Contest 350 (小白来了)
    A-PastABCs思路:题意需要计算已经结束的比赛其中1~349属于已经结束的比赛,其中316没有计算进去模拟即可Code:#include<bits/stdc++.h>usingnamespacestd;intmain(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);strings;cin>......
  • AtCoder Beginner Contest 350 解题报告
    AtCoderBeginnerContest350A-PastABCs当且仅当串为\(\texttt{ABC000},\texttt{ABC316},\texttt{ABC350}\sim\texttt{ABC999}\)时输出\(\texttt{No}\)。(本人因\(000\)挂了一发。)#include<bits/stdc++.h>usingnamespacestd;intmain(){ ios::sync_with_......
  • F - Subsequence LCM
    F-SubsequenceLCMProblemStatementYouaregivenasequenceofpositiveintegers$A=(A_1,A_2,\dots,A_N)$oflength$N$andapositiveinteger$M$.Findthenumber,modulo$998244353$,ofnon-emptyandnotnecessarilycontiguoussubsequencesof$A$suc......
  • [491] Non-decreasing Subsequences
    算法助手用户:这个题目有什么好的思路吗?“Givenanintegerarraynums,returnallthedifferentpossiblenon-decreasingsubsequencesofthegivenarraywithatleasttwoelements.Youmayreturntheanswerinanyorder.”我的代码是这样的:/**@lcapp=leetcod......