Namomo Winter Camp 2023 Day 2
A - Mix Juice
排个序再求和就好
#include<bits/stdc++.h>
using namespace std;
int read(){...}
int32_t main(){
int n = read() , k = read();
vector<int> a(n);
for( auto & i : a ) i = read();
sort( a.begin() , a.end() );
int res = 0;
for( int i = 0 ; i < k ; i ++ ) res += a[i];
cout << res << "\n";
return 0;
}
B - One Quadrillion and One Dalmatians
进制转换,因为没有0所以要特殊处理一下
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int32_t main(){
int n; cin >> n;
vector<int>res;
do{
n-- , res.push_back( n%26 ) , n /= 26;
}while( n > 0 );
std::reverse(res.begin(), res.end());
for( int i : res )
cout << char(i+'a');
}
C - Replacing
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int x = 0 , f = 1 , ch = getchar();
while( (ch < '0' || ch > '9') && ch != '-' ) ch = getchar();
if( ch == '-' ) f = -1 , ch = getchar();
while( ch >= '0' && ch <= '9' ) x = ( x << 3 ) + ( x << 1 ) + ch - '0' , ch = getchar();
return x * f;
}
const int N = 1e5+5;
int v[N] , res = 0;
int32_t main(){
for( int n = read() , x ; n ; n -- ) x = read() , res += x , v[x] ++;
for( int q = read() , b , c ; q ; q -- )
b = read() , c = read() , res += (c-b) * v[b] , v[c] += v[b] , v[b] = 0 , printf("%lld\n" , res );
return 0;
}
D - Red Scarf
因为保证了偶数,所以把所有的数异或起来就等于原数每个数异或起来。然后再异或剩余的数就是原数
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int x = 0 , f = 1 , ch = getchar();
while( (ch < '0' || ch > '9') && ch != '-' ) ch = getchar();
if( ch == '-' ) f = -1 , ch = getchar();
while( ch >= '0' && ch <= '9' ) x = ( x << 3 ) + ( x << 1 ) + ch - '0' , ch = getchar();
return x * f;
}
int32_t main(){
int n = read() , sum = 0;
vector<int> a(n);
for( auto & i : a ) i = read() , sum ^= i;
for( auto i : a ) printf("%lld " , sum ^ i );
return 0;
}
F - Minor Change
#include<bits/stdc++.h>
#define int unsigned long long
typedef long long ll;
using namespace std;
int32_t main(){
ios::sync_with_stdio(false) , cin.tie();
string s , t;
int res = 0;
cin >> s >> t;
for( int i = 0 ; i < s.size() ; i ++ )
if( s[i] != t[i] ) res ++;
cout << res << "\n";
return 0;
}
G - Tsundoku
排序,求前缀和,枚举一个买多少二分另一个能买多少,然后判断一下就好
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
int x = 0 , f = 1 , ch = getchar();
while( (ch < '0' || ch > '9') && ch != '-' ) ch = getchar();
if( ch == '-' ) f = -1 , ch = getchar();
while( ch >= '0' && ch <= '9' ) x = ( x << 3 ) + ( x << 1 ) + ch - '0' , ch = getchar();
return x * f;
}
int32_t main(){
int n = read() , m = read() , k = read() , res = 0;
vector<int> a(n+1) , b(m+1);
for( int i = 1 ; i <= n ; i ++ ) a[i] = read();
for( int i = 1 ; i <= m ; i ++ ) b[i] = read();
for( int i = 1 ; i <= n ; i ++ ) a[i] += a[i-1];
for( int i = 1 ; i <= m ; i ++ ) b[i] += b[i-1];
for( int i = 0 , t , j ; i <= n ; i ++ ){
if( a[i] > k ) break;
t = k - a[i];
j = upper_bound( b.begin() , b.end() , t ) - b.begin() - 1;
res = max( res , i + j );
}
cout << res << "\n";
}
H - Sum of Divisors
做一个类似埃筛的东西,去标记一个数的倍数,这样可以\(O(n\log n)\)的求出所有数有多少个因子,然后再\(O(n)\)的算一边就好
#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main(){
int n;
cin >> n;
vector<int> f(n+1);
for( int i = 1 ; i <= n ; i ++ )
for( int j = i ; j <= n ; j += i ) f[j]++;
int res = 0;
for( int i = 1 ; i <= n ; i ++ ) res += i * f[i];
cout << res << "\n";
return 0;
}
I - NEQ(补题)
首先这题赛时没出就是自己傻逼了,预处理阶乘的是否忘记取模
首先我们不考虑\(B_i\),只考虑\(A_i\)的情况,显然有\(A_m^n\)中情况,然后我们要计算出对于每一个序列\(A\)有多少个序列\(B\),易知对于每种序列\(A\)符合条件的序列\(B\)的数量是相等的。
直接计算合法的序列\(B\)比较困难,我们用序列\(B\)的总数减去不合法的数量就是合法的数量。
用到了一个组合容斥,暂时还不太会,后面补一下。
\[cnt=\sum_{k=0}^n (-1)^k\times C(n,k) \times A(m-k,n-k)
\]
最后的答案就是\(cnt\times A(m,n)\)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5+5 , mod = 1e9+7;
int fact[N] , invFact[N] , n , m , res = 0;
int power(int x,int y)
{
int ans =1;
while(y)
{
if( y & 1 ) ans = ans * x % mod;
x= x * x %mod;
y>>=1;
}
return ans;
}
int inv( int x ){
return power( x , mod - 2 );
}
int A( int x , int y ){
return fact[x] * invFact[x-y] % mod;
}
int C( int x , int y ){
return fact[x] * invFact[x-y] % mod * invFact[y] % mod;
}
int32_t main(){
fact[0] = 1 , invFact[0] = inv(1);
for( int i = 1 ; i < N ; i ++ ) fact[i] = fact[i-1] * i % mod , invFact[i] = inv(fact[i]);
cin >> n >> m;
for(int i = 0;i <= n;i++){
if(i&1) res=(res-C(n,i)*A(m-i,n-i)%mod+mod)%mod;
else res=(res+C(n,i)*A(m-i,n-i)%mod)%mod;
}
cout << res * A(m,n) % mod;
}