目录
A. Random Number Checker
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Eileen has written a random number generator recently to randomly generate odd and even numbers. Now, she wants to check whether the generator is good. She used it to generate NN numbers. If the difference between the number of occurrences of odd and even numbers is at most one, she thinks the generator is good.
Please write a program to help her.
Input
The first line of contains an integer N(1≤N≤105)N(1≤N≤105), indicating the number of numbers generated.
The next line contains NN integers A1,A2,…AN(1≤Ai≤109)A1,A2,…AN(1≤Ai≤109), indicating the NN generated numbers.
Output
If the generator is good, output "Good"; otherwise output "Not Good" (no quotes).
Examples
input
Copy
5 1 2 3 4 5
output
Copy
Good
input
Copy
5 1 1 3 4 5
output
Copy
Not Good
import java.io.*;
import java.util.*;
public class Main {
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader(new InputStreamReader(in),16834);
//eat("");
}
//public
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
int cnt=0;
int temp=n;
while(n-->0) {
int x=cin.nextInt();
if(x%2==0)cnt++;
}
// System.out.println(n/2);
if(cnt==temp/2||cnt==(temp+1)/2)System.out.println("Good");
else System.out.println("Not Good");
}
}
B. Arithmetic Exercise
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Carol is learning fractional division nowadays. But she has problem determining if the answer is correct. Please you help her find out the correct answer.
Input
A line contains three positive integers A,BA,B and KK: indicating the numerator, the denominator and the number of decimal places to be output.
It's guaranteed that 1≤A≤B≤103,1≤K≤1031≤A≤B≤103,1≤K≤103.
Output
Output one line contains a decimal ABAB with KK digits after the decimal point.
Please round the last decimal with round half up rule. Suppose the KK-th digit is xx and the K+1K+1-th digit is yy. If 5≤y≤95≤y≤9, round the KK-th digit to x+1x+1; otherwise, round it to xx
It's guaranteed that xx won't be 99 if 5≤y≤95≤y≤9.
Examples
input
Copy
1 2 2
output
Copy
0.50
input
Copy
10 99 5
output
Copy
0.10101
input
Copy
656 939 10
output
Copy
0.6986155485
input
Copy
1 3 20
output
Copy
0.33333333333333333333
思路:用一个数组用来记录整数位和k+1位保留位的数,然后判断k+1为是否要进行进位,然后从后往前判断直至无需进位为止。
import java.util.*;
import java.io.*;
public class Main {
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader(new InputStreamReader(in),16834);
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) {
int a=cin.nextInt();
int b=cin.nextInt();
int k=cin.nextInt();
int p[]=new int[k+10];
for(int i=0;i<=k+1;i++) {
int t=a/b;
int r=a%b;
p[i]=t;
a=r*10;
}
if(p[k+1]>=5)p[k]++;
int j=k;
while(j>=0&&p[j]==10) {
p[j]=0;
p[j-1]++;
}
for(int i=0;i<=k;i++) {
out.print(p[i]);
if(i==0)out.print(".");
}
out.flush();
}
}
E. Great Detective TJC
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, Detective TJC received a commission. In order to complete the task, he needs to solve a puzzle:
Given an array AA consisting of NN positive integers A1,A2,…AnA1,A2,…An, you need to find 22 distinct indices i,j(i≠j)i,j(i≠j), such that Ai⊕Aj=1Ai⊕Aj=1, where ⊕⊕ denotes the bitwise XOR operation. The bitwise XOR is a binary operation that takes two bit patterns of equal length and performs the logical exclusive OR operation on each pair of corresponding bits. The result in each position is 11 if only one of the bits is 11, but will be 00 if both are 00 or both are 11. For example: 10(10102)⊕12(11002)=6(01102)10(10102)⊕12(11002)=6(01102).
However, TJC doesn't like to find anything in chaotic numbers, because it reminds him of his bedroom. So he comes to ask you for help. You just need to tell him if there is an answer.
Input
The first line contains an integer T(1≤T≤100)T(1≤T≤100), indicating the number of test cases you need to solve.
The first line of each test case contains an integer N(1≤N≤105)N(1≤N≤105), indicating the number of elements in the array. The next line contains NN space-separated integers A1,A2,…An(1≤Ai≤109)A1,A2,…An(1≤Ai≤109), indicating the elements of the array AA.
It is guaranteed that the sum of NN over all test cases does not exceed 5×1055×105.
Output
For each test case, if there exists 22 distinct indices i,ji,j such that Ai⊕Aj=1Ai⊕Aj=1, output "Yes". Otherwise, print "No" (no quotes).
Example
input
Copy
2 4 5 1 3 4 2 5 6
output
Copy
Yes No
思路:通过题意我们可以知道,只有两个数二进制表示的最后一位相异时异或才可得到1,那么我们可以用两个集合,一个存取奇数,一个存取偶数,当奇数-1存在于偶数集合中,那么输出"Yes",否则输出"No".
import java.io.*;
import java.util.*;
public class Main {
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader(new InputStreamReader(in),16834);
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) {
// TODO Auto-generated method stub
// Scanner cin=new Scanner(System.in);
int t=cin.nextInt();
Set<Integer> st1=new HashSet<>();
Set<Integer> st2=new HashSet<>();
while(t-->0) {
st1.clear();st2.clear();
int n=cin.nextInt();
int x;
for(int i=0;i<n;i++) {
x=cin.nextInt();
if((x&1)==1)st1.add(x);
else st2.add(x);
}
int f=0;
for(int i:st1) {
if(st2.contains(i-1)) {
f=1;
break;
}
}
if(f==0)out.println("No");
else out.println("Yes");
}
out.flush();
}
}
H. Visit the Park
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Little Msacywy is visiting a beautiful park! The park has NN scenic spots, and there are MM bidirectional roads between them. In other words, the park can be considered as an undirected graph.
Before his visit, Msacywy had already planned his visiting path. The path is a sequence of scenic spots {A1,A2,…AK}{A1,A2,…AK}. He will start at spot A1A1, and go strictly along the path, which means when he is at spot AiAi, he will find a road between AiAi and Ai+1Ai+1 and move to Ai+1Ai+1 with this road. If there are several roads between AiAi and Ai+1Ai+1, he will choose a road randomly with equal probability. Note that Msacywy can reach a spot more than once.
Moreover, each road has a digit between 11 and 99. When Msacywy passes through the ii-th road, he will write down the digit of this road from left to right on his paper. Finally, there will be an integer of k−1k−1 digits on his paper.
Given the structure of the park and the planned path of Msacywy. Find the expected number of the integer written on the paper. We can show that the answer can be written in the form ABAB where A,BA,B are relatively prime numbers. Output the value of A×B−1A×B−1 modulo 998244853998244853.
Input
The first line of input file contains three integers N,MN,M and K(2≤N,M,K≤3×105)K(2≤N,M,K≤3×105), indicating the number of spots, the number of roads and the length of planned path.
The following MM lines describe all the roads. Each line contains three integers u,v(1≤u,v≤N,u≠v)u,v(1≤u,v≤N,u≠v) and w(1≤w≤9)w(1≤w≤9), representing a road between uu and vv with digit ww.
The last line contains KK integers A1,A2,…AK(1≤Ai≤N,Ai≠Ai+1)A1,A2,…AK(1≤Ai≤N,Ai≠Ai+1), representing the planned path of Msacywy.
Output
If Msacywy can go along the path, output a single integer, the expected number modulo 998244853998244853; otherwise, output a line "Stupid Msacywy!" (without quotes).
Examples
input
Copy
3 5 3 1 2 1 1 2 2 2 1 2 2 3 4 3 2 1 1 2 3
output
Copy
831870730
input
Copy
3 3 5 1 2 1 2 3 4 3 2 1 1 2 3 2 3
output
Copy
499123704
input
Copy
3 4 4 1 2 1 1 2 2 2 1 3 2 3 5 1 2 3 1
output
Copy
Stupid Msacywy!
Note
For the first sample, the integer could be 44 different values:
- 1414 with probability 1616;
- 1111 with probability 1616;
- 2424 with probability 1313;
- 2121 with probability 1313.
So the answer is 14×16+11×16+24×13+21×13=115614×16+11×16+24×13+21×13=1156.
思路:本题是求期望,我们可知,所以本题只需要使用这个式子配以乘法逆元便可以求解。
import java.util.*;
import java.io.*;
public class Main {
static class r{
int a,b;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + a;
result = prime * result + b;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
r other = (r) obj;
if (a != other.a)
return false;
if (b != other.b)
return false;
return true;
}
}
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader(new InputStreamReader(in),16834);
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int mod=998244853;
public static void main(String[] args) {
HashMap<r,Vector>map=new HashMap<>();
int n=cin.nextInt(),m=cin.nextInt(),k=cin.nextInt();
for(int i=0;i<m;i++) {
int u=cin.nextInt();
int v=cin.nextInt();
int w=cin.nextInt();
r a=new r();
a.a=Math.min(u,v);a.b=Math.max(u,v);
Vector<Integer> q=new Vector<>();
if(map.containsKey(a))q=map.get(a);
q.add(w);
map.put(a, q);
}
long ans=0,pos=1,t=qmi(10);
boolean ok=true;
for(int i=1;i<k-1;i++)pos=pos*10%mod;
int a=cin.nextInt(),b=0;
for(int i=1;i<k;i++) {
b=cin.nextInt();
r c=new r();
c.a=Math.min(a, b);c.b=Math.max(a, b);
if(!map.containsKey(c)) {
ok=false;
break;
}else {
Vector<Integer>q=map.get(c);
long ss=qmi(q.size());
for(int j=0;j<q.size();j++) {
// out.println(q.get(j)+" "+pos);
ans=(ans+((pos*q.get(j))%mod*ss)%mod)%mod;
}
pos=pos*t%mod;
// out.println(t);
}
a=b;
}
if(ok)out.println(ans%mod);
else out.println("Stupid Msacywy!");
out.flush();
}
private static long qmi(long a) {
// TODO Auto-generated method stub
long res=1;
long b=mod-2;
while(b>0) {
if(b%2==1)res=res*a%mod;
a=a*a%mod;
b/=2;
}
return res%mod;
}
}
K. Bracket Sequence
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A balanced bracket sequence is a string consisting of only brackets ("((" and "))").
One day, Carol asked Bella the number of balanced bracket sequences of length 2N2N (NN pairs of brackets). As a mental arithmetic master, she calculated the answer immediately. So Carol asked a harder problem: the number of balanced bracket sequences of length 2N2N (NN pairs of brackets) with KK types of bracket. Bella can't answer it immediately, please help her.
Formally you can define balanced bracket sequence with KK types of bracket with:
- ϵϵ (the empty string) is a balanced bracket sequence;
- If AA is a balanced bracket sequence, then so is lArlAr, where ll indicates the opening bracket and rr indicates the closing bracket. lrlr must match with each other and forms one of the KK types of bracket.
- If AA and BB are balanced bracket sequences, then so is ABAB.
For example, if we have two types of bracket "()()" and "[][]", "()[]()[]", "[()][()]" and "([])([])" are balanced bracket sequences, and "[(])[(])" and "([)]([)]" are not. Because "(](]" and "[)[)" can't form can't match with each other.
Input
A line contains two integers NN and KK: indicating the number of pairs and the number of types.
It's guaranteed that 1≤K,N≤1051≤K,N≤105.
Output
Output one line contains a integer: the number of balanced bracket sequences of length 2N2N (NN pairs of brackets) with KK types of bracket.
Because the answer may be too big, output it modulo 109+7109+7.
Examples
input
Copy
1 2
output
Copy
2
input
Copy
2 2
output
Copy
8
input
Copy
24 20
output
Copy
35996330
思路:本题是一个卡特兰数,组合数+乘法逆元。(来自队友的截图嘻嘻~)
import java.util.*;
import java.io.*;
public class Main {
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader(new InputStreamReader(in),16834);
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N=200010;
static long f[]=new long[N];
static long inv[]=new long[N];
static int mod=(int) (1e9+7);
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=cin.nextInt(),k=cin.nextInt();
f[0]=inv[0]=1;
for(int i=1;i<N;i++) {
f[i]=f[i-1]*i%mod;
inv[i]=inv[i-1]*qmi(i,mod-2)%mod;
}
long ans=qmi(k,n)%mod;
ans=(ans*f[2*n]%mod*inv[n]%mod*inv[n]%mod*qmi(n+1,mod-2)%mod)%mod;
out.println(ans%mod);
out.flush();
}
private static long qmi(long a, int b) {
// TODO Auto-generated method stub
long ans=1;
while(b>0) {
if(b%2==1)ans=(ans%mod*a%mod)%mod;
a=((long)a%mod*a%mod)%mod;
b/=2;
}
return (int) (ans%mod);
}
}
L. Suzuran Loves String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Miss Suzuran is the light of us.
Tonight Suzuran is learning string data structures. Learning the suffix tree, she wonders how long is the 'diameter' of a suffix tree. Formally, Suzuran has a string SS of length nn. Now she can apply the following operations on a string ss.
- Delete one character from the end of ss.
- Add one character to the end of ss.
She defines the distance of suffix AA and suffix BB, which is denoted as d(A,B)d(A,B), the mininum number of operations you need to apply on AA to make it same as BB.
Of course, string SS has nn different suffixes. Suzuran refers to the suffix begins at ii-th position as si(0≤i<n)si(0≤i<n). She wonders the maximum value of d(si,sj)(0≤i<j<n)d(si,sj)(0≤i<j<n). She tries to solve this problem with string data structures, but it seems too complicated to her.
So Suzuran comes to you. Try to teach Suzuran a simple way to solve it, so that she could go bed on time and presenting Doctor Kal'sist from madness and you from sleeping on the bridge of the ship.
By the way, as a gentle girl, Suzuran tells you the definition of "suffix": if string AA is a suffix of string BB, you could obtain AA by deleting several (possibly zero) characters from the beginning of BB.
Input
The first line contains an integer TT, refering to the number of test cases.
For each test case, there is only one line, containing a string SS that Suzuran brings to you.
The strings only contain lowercase English letters.
It guaranteened that 2≤|S|≤1000000,∑|S|≤50000002≤|S|≤1000000,∑|S|≤5000000.
Output
For each test case, output one integer, refering to the maximum value of d(si,sj)(0≤i<j<n)d(si,sj)(0≤i<j<n).
Example
input
Copy
1 doctor
output
Copy
11
Note
For string "doctor", you pick suffix "doctor" and suffix "octor". And you need to delete 'r', 'o', 't', 'c', 'o', 'd' from the end of string "doctor" one by one and then add 'o', 'c', 't', 'o', 'r' to the end to make suffix "doctor" the same as suffix "octor". There is no way to pick suffixes to get a bigger distance, so the answer is 11.
思路:通过题意我们可以分为三种情况:
①字符串s前面无重复,例:doctor
这时候最大值为s.length*2-1;
②字符串前面存在重复,但是字符串不止包含一个字母,例:ooobbsdf
这时最大值为s.length*2-j;
③字符串仅包含一个字母,例:aaaaaa
这时最大值为s.length-1;
import java.io.*;
import java.util.*;
public class Main {
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader(new InputStreamReader(in),16834);
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) {
// TODO Auto-generated method stub
int t=cin.nextInt();
while(t-->0) {
String s=cin.next();
char c[]=s.toCharArray();
int flag=0,cnt=0,j=0,i=0;
for(i=1;i<c.length;i++) {
if(c[i]==c[i-1]) {
flag=1;
cnt++;
}else {
j=i;break;
}
}
// out.println(i);
if(i==s.length()&&flag==1) {
out.println(s.length()-1);
}else if(flag==0)out.println((s.length()*2-1));
else out.println((s.length()*2-j));
out.flush();
}
}
}
M. Sequence
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Chiang has a magic sequence which consists of nn integers, but she doesn't understand the subtelty of the sequence, like the average, mode, variance, range value...
Her teammate tells her the interesting value of a sequence is the product of range value and sequence length. Now she is staring at this sequence wondering its interesting value. Chiang has learned a lot of knowledge so she wants to share some with you:
- cc is the product of aa and bb if and only if c=a×bc=a×b.
- Range value is the difference between the maximum and the minimum of the sequence.
Input
The first line of the input contains a single integer n(1≤n≤10000)n(1≤n≤10000) indicating the number of integers.
Next line contains nn integers A1,A2,…,AnA1,A2,…,An which refers to the sequence (−100000≤Ai≤100000−100000≤Ai≤100000).
Output
A single integer refers to the interesting value of the sequence.
Example
input
Copy
4 3 -1 2 4
output
Copy
20
import java.util.*;
import java.io.*;
public class Main {
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader(new InputStreamReader(in),16834);
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner cin=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) {
int n=cin.nextInt();
int min=Integer.MAX_VALUE,max=Integer.MIN_VALUE;
for(int i=0;i<n;i++) {
int x=cin.nextInt();
min=Math.min(min, x);
max=Math.max(max, x);
}
long t=(long)(max-min);
out.println((long)(t*n));
out.flush();
}
}