AB没啥好说的
C - Count Order /
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 300300 points
Problem Statement
We have two permutations PP and QQ of size NN (that is, PP and QQ are both rearrangements of (1, 2, ..., N)(1, 2, ..., N)).
There are N!N! possible permutations of size NN. Among them, let PP and QQ be the aa-th and bb-th lexicographically smallest permutations, respectively. Find |a−b||a−b|.
Notes
For two sequences XX and YY, XX is said to be lexicographically smaller than YY if and only if there exists an integer kk such that Xi=Yi (1≤i<k)Xi=Yi (1≤i<k) and Xk<YkXk<Yk.
Constraints
- 2≤N≤82≤N≤8
- PP and QQ are permutations of size NN.
Input
Input is given from Standard Input in the following format:
NN P1P1 P2P2 ...... PNPN Q1Q1 Q2Q2 ...... QNQN
Output
Print |a−b||a−b|.
用康托展开,这里数据很小也可以直接STL跑next_permutation。。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 5 int jc[10]={1}; 6 int a[10],b[10],n; 7 int cal(int a[],int n){ 8 int res=0; 9 for(int i=1;i<=n;++i){ 10 int c=0; 11 for(int j=i+1;j<=n;++j)c+=(a[j]<a[i]); 12 res+=c*jc[n-i]; 13 }return res; 14 } 15 int main(){ 16 for(int i=1;i<=9;++i)jc[i]=i*jc[i-1]; 17 cin>>n; 18 for(int i=1;i<=n;++i)cin>>a[i]; 19 for(int i=1;i<=n;++i)cin>>b[i]; 20 cout<<abs(cal(b,n)-cal(a,n))<<endl; 21 return 0; 22 }
D - Semi Common Multiple /
Time Limit: 2 sec / Memory Limit: 1024 MB
Score : 400 points
Problem Statement
Given are a sequence A=a1,a2,......aNA=a1,a2,......aN of NN positive even numbers, and an integer MM.
Let a semi-common multiple of AA be a positive integer XX that satisfies the following condition for every kk (1≤k≤N)(1≤k≤N):
- There exists a non-negative integer pp such that X=ak×(p+0.5)X=ak×(p+0.5).
Find the number of semi-common multiples of AA among the integers between 11 and MM (inclusive).
Constraints
- 1≤N≤1051≤N≤105
- 1≤M≤1091≤M≤109
- 2≤ai≤1092≤ai≤109
- aiai is an even number.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
NN MM a1a1 a2a2 ...... aNaN
Output
Print the number of semi-common multiples of AA among the integers between 11 and MM (inclusive).
X=ai*(p+0.5)可以看作是X=ai/2*(2p+1),令bi=ai/2的话,那么显然X满足X%bi==0且X/bi是个奇数。
令g=lcm(b1,b2...bn)的话,满足条件的数只可能是k*g,假设g不满足题意那么k*g也不会满足题意(g不满足,即存在一个b使得g/b是偶数,那显然k*g/b也是偶数,也不符合)。
假设g满足题意那么只有在k为奇数得情况下k*g满足题意,这时候统计下小于等于M且满足上述条件的数即可。
还要注意求lcm得过程可能会爆LL,为此只要当前的lcm>M那么答案显然就是零了,特判下。
这种数学规律题神烦,代码很丑= =
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 5 LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);} 6 LL lcm(LL a,LL b){return a/gcd(a,b)*b;} 7 LL N,M,a[100010]; 8 int main(){ 9 cin>>N>>M; 10 LL g=1,ok=1; 11 for(int i=1;i<=N;++i){ 12 cin>>a[i]; 13 a[i]/=2; 14 if(i==1)g=a[i]; 15 g=lcm(g,a[i]); 16 if(g>M)ok=0; 17 } 18 for(int i=1;i<=N;++i){ 19 if(g/a[i]%2==0){ok=0;break;} 20 } 21 if(!ok){ 22 cout<<0<<endl; 23 return 0; 24 } 25 LL ans=0; 26 LL b=M/g; 27 ans=b/2; 28 if(b&1)ans++; 29 cout<<ans<<endl; 30 return 0; 31 }