K. Bracket Sequence
卡特兰数 模版
// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;
/**
* 题目地址
*
*/
// xixi♡西
public class Main {
static IoScanner sc = new IoScanner();
// static Scanner sc=new Scanner(System.in)
static final int MOD = (int) (1e9 + 7);
// static final int mod = (int) (998244353);
static int n;
static int arr[];
static boolean visited[];
static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
static int map[][];
/**
* @throws IOException
*/
private static void solve() throws IOException {
long n=sc.nextLong();
long k=sc.nextLong();
// 每一种
long sum1=1;
// for(int i=0;i<n;i++) {
// sum1=Math.pow(n, k) ;
// }
sum1*=powMod(k, n ,MOD) ;
long sum2=catalanFormula(n);
// dduoln(sum1);
// dduoln(sum2);
dduoln((sum1*sum2)%MOD);
}
// 卡特兰数
// 方法3:显式公式计算(带取模,使用逆元)
public static long catalanFormula(long n) {
if (n == 0) {
return 1;
}
// 计算组合数 C(2n, n) / (n+1) mod MOD
long result = 1;
// 计算 C(2n, n) mod MOD
for (int i = 1; i <= n; i++) {
result = result * (n + i) % MOD;
result = result * modInverse(i, MOD) % MOD;
}
// 除以 (n+1) 等同于乘以其逆元
return result * modInverse(n + 1, MOD) % MOD;
}
// 计算 a 在模 m 下的逆元(要求 m 为质数)
private static long modInverse(long a, long m) {
return powMod(a, m - 2, m);
}
// 快速幂取模
private static long powMod(long base, long exp, long mod) {
long result = 1;
base %= mod;
while (exp > 0) {
if (exp % 2 == 1) {
result = result * base % mod;
}
base = base * base % mod;
exp >>= 1;
}
return result;
}
/**
100
1 2
2 2
24 20
*/
public static void main(String[] args) throws Exception {
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
static <T> void dduo(T t) {
System.out.print(t);
}
static <T> void dduoln() {
System.out.println("");
}
static <T> void dduoln(T t) {
System.out.println(t);
}
}
/**
* IoScanner类
*
* @author Dduo
* @version 1.0
* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。
*/
class IoScanner {
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public IoScanner() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException {
return bf.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
public BigDecimal nextDecimal() throws IOException {
return new BigDecimal(next());
}
}
L. Suzuran Loves String
// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;
/**
* 题目地址
*
*/
// xixi♡西
public class Main {
static IoScanner sc = new IoScanner();
// static Scanner sc=new Scanner(System.in)
// static final int mod = (int) (1e9 + 7);
static final int mod = (int) (998244353);
static int n;
static int arr[];
static boolean visited[];
static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
static int map[][];
/**
* @throws IOException
*/
private static void solve() throws IOException {
String str = sc.next();
int n=str.length();
HashSet<Character>hs=new HashSet<>();
int p=-1;
for(int i=0;i<str.length();i++) {
hs.add(str.charAt(i));
if(p==-1&&hs.size()>=2) {
p=i;
}
}
// dduoln(p);
// dduoln(n);
if(hs.size()==1) {
dduoln(str.length()-1);
}else {
dduoln((n-p)+(n));
}
}
/**
*/
public static void main(String[] args) throws Exception {
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
static <T> void dduo(T t) {
System.out.print(t);
}
static <T> void dduoln() {
System.out.println("");
}
static <T> void dduoln(T t) {
System.out.println(t);
}
}
/**
* IoScanner类
*
* @author Dduo
* @version 1.0
* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。
*/
class IoScanner {
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public IoScanner() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException {
return bf.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
public BigDecimal nextDecimal() throws IOException {
return new BigDecimal(next());
}
}
B. Arithmetic Exercise
// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;
/**
* 题目地址
*
*/
// xixi♡西
public class Main {
static IoScanner sc = new IoScanner();
// static Scanner sc=new Scanner(System.in)
// static final int mod = (int) (1e9 + 7);
static final int mod = (int) (998244353);
static int n;
static int arr[];
static boolean visited[];
static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
static int map[][];
/**
* @throws IOException
*/
private static void solve() throws IOException {
long a=sc.nextLong();
long b=sc.nextLong();
long k=sc.nextLong();
BigDecimal num1 = new BigDecimal(a);
BigDecimal num2 = new BigDecimal(b);
BigDecimal quotient = num1.divide(num2, (int) k, RoundingMode.HALF_UP);
dduoln(quotient);
}
/**
*/
public static void main(String[] args) throws Exception {
int t = 1;
// t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
static <T> void dduo(T t) {
System.out.print(t);
}
static <T> void dduoln() {
System.out.println("");
}
static <T> void dduoln(T t) {
System.out.println(t);
}
}
/**
* IoScanner类
*
* @author Dduo
* @version 1.0
* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。
*/
class IoScanner {
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public IoScanner() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException {
return bf.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
public BigDecimal nextDecimal() throws IOException {
return new BigDecimal(next());
}
}
E. Great Detective TJC
// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;
/**
* 题目地址
*
*/
// xixi♡西
public class Main {
static IoScanner sc = new IoScanner();
// static Scanner sc=new Scanner(System.in)
// static final int mod = (int) (1e9 + 7);
static final int mod = (int) (998244353);
static int n;
static int arr[];
static boolean visited[];
static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
static int map[][];
/**
* @throws IOException
*/
private static void solve() throws IOException {
int n=sc.nextInt();
long arr[]=new long[n];
HashSet<Long> hs =new HashSet<>();
for(int i=0;i<n;i++) {
arr[i]=sc.nextLong();
hs.add(arr[i]);
}
for(int i=0;i<n;i++) {
if(arr[i]%2==0&&hs.contains(arr[i]+1)==true) {
dduoln("Yes");
return;
}
}
dduoln("No");
}
/**
*/
public static void main(String[] args) throws Exception {
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
static <T> void dduo(T t) {
System.out.print(t);
}
static <T> void dduoln() {
System.out.println("");
}
static <T> void dduoln(T t) {
System.out.println(t);
}
}
/**
* IoScanner类
*
* @author Dduo
* @version 1.0
* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。
*/
class IoScanner {
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public IoScanner() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException {
return bf.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
public BigDecimal nextDecimal() throws IOException {
return new BigDecimal(next());
}
}
A. Random Number Checker
// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;
/**
* 题目地址
*
*/
// xixi♡西
public class Main {
static IoScanner sc = new IoScanner();
// static Scanner sc=new Scanner(System.in)
// static final int mod = (int) (1e9 + 7);
static final int mod = (int) (998244353);
static int n;
static int arr[];
static boolean visited[];
static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
static int map[][];
/**
* @throws IOException
*/
private static void solve() throws IOException {
int n=sc.nextInt();
int ji=0;
int ou=0;
for(int i=0;i<n;i++) {
int ans=sc.nextInt();
if(ans%2==0) {
ou++;
}else {
ji++;
}
}
if(Math.abs(ji-ou)<=1) {
dduoln("Good");
}else {
dduoln("Not Good");
}
}
/**
*/
public static void main(String[] args) throws Exception {
int t = 1;
// t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
static <T> void dduo(T t) {
System.out.print(t);
}
static <T> void dduoln() {
System.out.println("");
}
static <T> void dduoln(T t) {
System.out.println(t);
}
}
/**
* IoScanner类
*
* @author Dduo
* @version 1.0
* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。
*/
class IoScanner {
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public IoScanner() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException {
return bf.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
public BigDecimal nextDecimal() throws IOException {
return new BigDecimal(next());
}
}
M. Sequence
// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;
/**
* 题目地址
*
*/
// xixi♡西
public class Main {
static IoScanner sc = new IoScanner();
// static Scanner sc=new Scanner(System.in)
// static final int mod = (int) (1e9 + 7);
static final int mod = (int) (998244353);
static int n;
static int arr[];
static boolean visited[];
static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
static int map[][];
/**
* @throws IOException
*/
private static void solve() throws IOException {
int n=sc.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextLong();
}
long max=Long.MIN_VALUE;
long min=Long.MAX_VALUE;
for(int i=0;i<n;i++) {
max=Math.max(max, arr[i]);
min=Math.min(min, arr[i]);
}
dduoln( (max-min)*n );
}
/**
*/
public static void main(String[] args) throws Exception {
int t = 1;
// t = sc.nextInt();
while (t-- > 0) {
solve();
}
}
static <T> void dduo(T t) {
System.out.print(t);
}
static <T> void dduoln() {
System.out.println("");
}
static <T> void dduoln(T t) {
System.out.println(t);
}
}
/**
* IoScanner类
*
* @author Dduo
* @version 1.0
* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。
*/
class IoScanner {
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public IoScanner() {
bf = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException {
return bf.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
public BigDecimal nextDecimal() throws IOException {
return new BigDecimal(next());
}
}