- A. New Year and Naming
- time limit per test
- 1 second
- memory limit per test
- 1024 megabytes
- input
- standard input
- output
- standard output
- Happy new year! The year 2020 is also known as Year Gyeongja (???, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.
There are two sequences of nn strings s1,s2,s3,...,sns1,s2,s3,...,sn and mm strings t1,t2,t3,...,tmt1,t2,t3,...,tm. These strings contain only lowercase letters. There might be duplicates among these strings.
Let's call a concatenation of strings xx and yy as the string that is obtained by writing down strings xx and yy one right after another without changing the order. For example, the concatenation of the strings"code"and"forces"is the string"codeforces".
The year 1 has a name which is the concatenation of the two strings s1s1 and t1t1. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.
For example, if n=3,m=4,s=n=3,m=4,s={"a", "b", "c"}, t=t= {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat.
You are given two sequences of strings of size nn and mm and also qq queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
- Input
- The first line contains two integers n,mn,m (1≤n,m≤201≤n,m≤20).
The next line contains nn strings s1,s2,...,sns1,s2,...,sn. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 11 and at most 1010.
The next line contains mm strings t1,t2,...,tmt1,t2,...,tm. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least 11 and at most 1010.
- Among the given n+mn+m strings may be duplicates (that is, they are not necessarily all different).
- The next line contains a single integer qq (1≤q≤20201≤q≤2020).
- In the next qq lines, an integer yy (1≤y≤1091≤y≤109) is given, denoting the year we want to know the name for.
- Output
- Print qq lines. For each line, print the name of the year as per the rule described above.
- Example
- input
- Copy
- 10 12
- sin im gye gap eul byeong jeong mu gi gyeong
- yu sul hae ja chuk in myo jin sa o mi sin
- 14
- 1
- 2
- 3
- 4
- 10
- 11
- 12
- 13
- 73
- 2016
- 2017
- 2018
- 2019
- 2020
- output
- Copy
- sinyu
- imsul
- gyehae
- gapja
- gyeongo
- sinmi
- imsin
- gyeyu
- gyeyu
- byeongsin
- jeongyu
- musul
- gihae
- gyeongja
- Note
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal.
就是对 S 和 T 分别循环查找第 y 个元素,
- #include <bits/stdc++.h>
- using namespace std;
- #define LL long long
- #define inf 0x3f3f3f3f
- #define pii pair<int,int>
- #define pb push_back
- string S[22],T[22];
- int n,m,q,y;
- int main()
- {
- //freopen("in.txt","r",stdin);
- cin>>n>>m;
- for(int i=1;i<=n;++i)cin>>S[i];
- for(int i=1;i<=m;++i)cin>>T[i];
- cin>>q;
- while(q--){
- cin>>y;
- int s=y%n,t=y%m;
- if(s==0)s=n;
- if(t==0)t=m;
- cout<<S[s]<<T[t]<<endl;
- }
- return 0;
- }
- View Code
- B. New Year and Ascent Sequence
- time limit per test
- 2 seconds
- memory limit per test
- 1024 megabytes
- input
- standard input
- output
- standard output
A sequence a=[a1,a2,...,al]a=[a1,a2,...,al] of length ll has an ascent if there exists a pair of indices (i,j)(i,j) such that 1≤i<j≤l1≤i<j≤l and ai<ajai<aj. For example, the sequence [0,2,0,2,0][0,2,0,2,0] has an ascent because of the pair (1,4)(1,4), but the sequence [4,3,3,3,1][4,3,3,3,1] doesn't have an ascent.
Let's call a concatenation of sequences pp and qq the sequence that is obtained by writing down sequences pp and qq one right after another without changing the order. For example, the concatenation of the [0,2,0,2,0][0,2,0,2,0] and [4,3,3,3,1][4,3,3,3,1] is the sequence [0,2,0,2,0,4,3,3,3,1][0,2,0,2,0,4,3,3,3,1]. The concatenation of sequences pp and qq is denoted as p+qp+q.
Gyeonggeun thinks that sequences with ascents bring luck. Therefore, he wants to make many such sequences for the new year. Gyeonggeun has nn sequences s1,s2,...,sns1,s2,...,sn which may have different lengths.
- Gyeonggeun will consider all n2n2 pairs of sequences sxsx and sysy (1≤x,y≤n1≤x,y≤n), and will check if its concatenation sx+sysx+sy has an ascent. Note that he may select the same sequence twice, and the order of selection matters.
- Please count the number of pairs (x,yx,y) of sequences s1,s2,...,sns1,s2,...,sn whose concatenation sx+sysx+sy contains an ascent.
- Input
- The first line contains the number nn (1≤n≤1000001≤n≤100000) denoting the number of sequences.
- The next nn lines contain the number lili (1≤li1≤li) denoting the length of sisi, followed by lili integers si,1,si,2,...,si,lisi,1,si,2,...,si,li (0≤si,j≤1060≤si,j≤106) denoting the sequence sisi.
It is guaranteed that the sum of all lili does not exceed 100000100000.
- Output
- Print a single integer, the number of pairs of sequences whose concatenation has an ascent.
- Examples
- input
- Copy
- 5
- 1 1
- 1 1
- 1 2
- 1 4
- 1 3
- output
- Copy
- 9
- input
- Copy
- 3
- 4 2 0 2 0
- 6 9 9 8 8 7 7
- 1 6
- output
- Copy
- 7
- input
- Copy
- 10
- 3 62 24 39
- 1 17
- 1 99
- 1 60
- 1 64
- 1 30
- 2 79 29
- 2 20 73
- 2 85 37
- 1 100
- output
- Copy
- 72
- Note
- For the first example, the following 99 arrays have an ascent: [1,2],[1,2],[1,3],[1,3],[1,4],[1,4],[2,3],[2,4],[3,4][1,2],[1,2],[1,3],[1,3],[1,4],[1,4],[2,3],[2,4],[3,4]. Arrays with the same contents are counted as their occurences.
我的思路是对于每一个数列只记录下他的 MAX 和 MIN 元素即可, 然后遍历每一个数列, 看有哪些数列的最小值是小于当前数列得最大值得, 这些数列的个数就是当前数列做的贡献.
注意有些数列本身就是特殊数列了, 就把他得 MIN 和 MAX 分别记为最小值 - 1 和最大值 INF 即可.
- #include <bits/stdc++.h>
- using namespace std;
- #define LL long long
- #define inf 0x3f3f3f3f
- #define pii pair<int,int>
- #define pb push_back
- int minn[100010],maxx[100010];
- int sum[1000100];
- int main()
- {
- //freopen("in.txt","r",stdin);
- int n,l;
- cin>>n;
- for(int i=1;i<=n;++i){
- cin>>l;
- int a,MIN=99999999,MAX=0,ok=0;
- for(int j=1;j<=l;++j){
- scanf("%d",&a);
- if(a>MIN)ok=1;
- MIN=min(MIN,a);
- MAX=max(MAX,a);
- }
- minn[i]=MIN;
- maxx[i]=MAX;
- if(ok){
- minn[i]=-1;
- maxx[i]=1000002;
- }
- sum[minn[i]+1]++;
- }
- for(int i=1;i<=1000002;++i)sum[i]+=sum[i-1];
- LL ans=0;
- for(int i=1;i<=n;++i){
- ans+=sum[maxx[i]];
- }
- cout<<ans<<endl;
- return 0;
- }
- View Code
- C. New Year and Permutation
- time limit per test
- 1 second
- memory limit per test
- 1024 megabytes
- input
- standard input
- output
- standard output
- Recall that the permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).
A sequence aa is a subsegment of a sequence bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. We will denote the subsegments as [l,r][l,r], where l,rl,r are two integers with 1≤l≤r≤n1≤l≤r≤n. This indicates the subsegment where l−1l−1 elements from the beginning and n−rn−r elements from the end are deleted from the sequence.
For a permutation p1,p2,...,pnp1,p2,...,pn, we define a framed segment as a subsegment [l,r][l,r] where max{pl,pl+1,...,pr}−min{pl,pl+1,...,pr}=r−lmax{pl,pl+1,...,pr}−min{pl,pl+1,...,pr}=r−l. For example, for the permutation (6,7,1,8,5,3,2,4)(6,7,1,8,5,3,2,4) some of its framed segments are: [1,2],[5,8],[6,7],[3,3],[8,8][1,2],[5,8],[6,7],[3,3],[8,8]. In particular, a subsegment [i,i][i,i] is always a framed segments for any ii between 11 and nn, inclusive.
We define the happiness of a permutation pp as the number of pairs (l,r)(l,r) such that 1≤l≤r≤n1≤l≤r≤n, and [l,r][l,r] is a framed segment. For example, the permutation [3,1,2][3,1,2] has happiness 55: all segments except [1,2][1,2] are framed segments.
- Given integers nn and mm, Jongwon wants to compute the sum of happiness for all permutations of length nn, modulo the prime number mm. Note that there exist n!n! (factorial of nn) different permutations of length nn.
- Input
- The only line contains two integers nn and mm (1≤n≤2500001≤n≤250000, 108≤m≤109108≤m≤109, mm is prime).
- Output
- Print rr (0≤r<m0≤r<m), the sum of happiness for all permutations of length nn, modulo a prime number mm.
- Examples
- input
- Copy
- 1 993244853
- output
- Copy
- 1
- input
- Copy
- 2 993244853
- output
- Copy
- 6
- input
- Copy
- 3 993244853
- output
- Copy
- 32
- input
- Copy
- 2019 993244853
- output
- Copy
- 923958830
- input
- Copy
- 2020 437122297
- output
- Copy
- 265955509
- Note
- For sample input n=3n=3, let's consider all permutations of length 33:
- [
- 1
- ,2,3]
- [1,2,3], all subsegments are framed segment. Happiness is
- 66.
- [
- 1
- ,3,2]
- [1,3,2], all subsegments except
- [1,2]
- [1,2] are framed segment. Happiness is
- 55.
- [
- 2
- ,1,3]
- [2,1,3], all subsegments except
- [2,3]
- [2,3] are framed segment. Happiness is
- 55.
- [
- 2
- ,3,1]
- [2,3,1], all subsegments except
- [2,3]
- [2,3] are framed segment. Happiness is
- 55.
- [
- 3
- ,1,2]
- [3,1,2], all subsegments except
- [1,2]
- [1,2] are framed segment. Happiness is
- 55.
- [
- 3
- ,2,1]
- [3,2,1], all subsegments are framed segment. Happiness is
- 66.
Thus, the sum of happiness is 6+5+5+5+5+6=326+5+5+5+5+6=32.
先考虑长度为 len 得时候这样的幸福度有多少, 此时 MAX-MIN=len-1 才满足题意, 有一个极为重要的点我没发现, 就是满足这些的数恰好都是连续的 (如 12,23,34,123,234,345...)
如果发现了这点就简单多了, len 知道以后, 不同种类的数有 n-len+1 种然后剩下考虑如何排列组合就好了,
- #include <bits/stdc++.h>
- using namespace std;
- const int MAXN = 250005;
- using lint = long long;
- using pi = pair<int, int>;
- int n, mod;
- lint fact[MAXN];
- int main(){
- fact[0] = 1;
- cin>> n>> mod;
- for(int i=1; i<=n; i++) fact[i] = fact[i-1] * i % mod;
- lint ret = 0;
- for(int i=1; i<=n; i++){
- ret += (n - i + 1) * (fact[i] * fact[n - i + 1] % mod);
- cout<<(n - i + 1) * (fact[i] * fact[n - i + 1] % mod)<<endl;
- ret %= mod;
- }
- cout << ret << endl;
- }
- View Code
来源: http://www.bubuko.com/infodetail-3366450.html