A. Meximization
code:
#include <string.h>
#include<algorithm>
#include <iostream>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
const int maxn=5e6+10;
int main()
{
cin.tie(0);std::ios::sync_with_stdio(false);
int t, a[105];
cin>>t;
while(t--){
int n;
cin>>n;
memset(a, 0, sizeof(a));
for(int i=0; i<n; i++) {
int x;
cin>>x;
a[x]++;
}
for(int i=0; i<=100; i++) {
if(a[i]) {
cout<<i<<" ";
a[i]--;
}
}
for(int i=0; i<=100; i++) {
while( a[i]-- ) {
cout<<i<<" ";
}
}
cout<<endl;
}
return 0;
}
B. M-arrays
题解:
可以根据%m的余数来对数组元素进行分类,两个数的能整除m的和可能大小不同,但这两个数%m的余数肯定是一定的。
再根据每种余数的数量计算结果。
code:
#include <string.h>
#include <math.h>
#include<algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int main()
{
cin.tie(0);std::ios::sync_with_stdio(false);
int t, a[maxn];
cin>>t;
while(t--){
int n, m, sum=0;
cin>>n>>m;
memset(a, 0, sizeof(a));
for(int i=0; i<n; i++){
int x;
cin>>x;
a[x%m]++;
}
if( a[0] ) sum++;
for(int i=1; i<=m/2; i++) {
if( a[i] && a[i]==a[m-i] ) sum++;
else sum+=abs(a[i]-a[m-i]);
}
cout<<sum<<endl;
}
return 0;
}
C1. k-LCM (easy version)
题解:
如何把n三分,因为最小公倍数<=n/2,可以先从两分考虑。
code:
法一:
#include <string.h>
#include<algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int main()
{
cin.tie(0);std::ios::sync_with_stdio(false);
int t, a[maxn];
cin>>t;
while(t--){
int n, k;
cin>>n>>k;
int x,y,z;
if( n&1 ) cout<<1<<" "<<(n-1)/2<<" "<<(n-1)/2;
else if( n%2==0 && n%4 )
cout<<2<<" "<<n/2-1<<" "<<n/2-1;
else if( n%4==0 )
cout<<n/2<<" "<<n/4<<" "<<n/4;
cout<<endl;
}
return 0;
}
法二:
#include <iostream>
using namespace std;
int main()
{
int t ;
cin>>t;
while(t--)
{
int n, k;
cin>>n>>k;
int x=1, y=n;
while(y%2==0)
{
x*=2;
y/=2;
}
if(y==1)
{
x/=2;
}
cout<<x<<" "<<(n-x)/2<<" "<<(n-x)/2;
cout<<endl;
}
return 0;
}
C2. k-LCM (hard version)
题解:
把n分为n-k+3和k-3。前三个按照easy版的方法做,后面均为1。
code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n, k;
cin>>n>>k;
n=n-(k-3);
for(int i=0; i<(k-3); i++) cout<<1<<" ";
k=3;
if(n%k==0) cout<<n/k<<" "<<n/k<<" "<<n/k<<endl;
else if(n%4 && n%2==0) cout<<2<<" "<<(n/2)-1<<" "<<(n/2)-1<<endl;
else if(n%4==0) cout<<n/2<<" "<<n/4<<" "<<n/4<<endl;
else cout<<1<<" "<<n/2<<" "<<n/2<<endl;
}
return 0;
}
E1. Square-free division (easy version)
题解:
判断是否是平方数,一开始想到的很快的方法就是打表,但这样就需逐两个求和判断和是不是平方数。直接超时。
试想,可以这样将一个数分解:n=a ^ x1 * b ^ x2 * c ^ x3 …
当xi均为偶数时,该数是平方数。
所以只需要判断两个数的乘积的xi是否均为偶数。一个个存储判断质因数过于麻烦。可以只记录次数的奇数的质因数的乘积,最后判断两个是否相等,(和会重)。
code:
#include <string.h>
#include<algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
int divide( int x ){
if( x==1 ) return 1;
int res=1;
for(int i=2; i*i<=x; i++){
int sum=0;
if( x%i==0 )
while( x%i==0 ){
sum++;
x/=i;
}
if( sum&1 ) res*=i;
}
res*=x;
return res;
}
int main()
{
cin.tie(0);std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
map <int, int> num;
int n, k, ans=0;
cin>>n>>k;
for(int i=0; i<n; i++){
int x;
cin>>x;
int y=divide(x);
if( num[y] ){
ans++;
num.clear();
}
num[y]++;
}
cout<<ans+1<<endl;
}
return 0;
}