B-小红的因子数_牛客周赛 Round 31 (nowcoder.com)
超时了(108.33/150)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
void solve() {int x;cin>>x;if(x==1){cout<<0;return ;}unordered_set<int>se;for(int i=2;i<=x;){if(x%i==0){se.insert(i);x/=i;}else i++;}cout<<se.size();
}signed main() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);solve();return 0;
}
ac代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
int f[1001];
void solve() {int x,k=0,cn=0;cin>>x;int s=x;for(int i=2;i*i<=s;i++){if(x%i==0){f[++k]=i;while(x%i==0){x/=i;}}}if(x>1)f[++k]=x;for(int i=0;i<1001;i++){if(f[i])cn++;}cout<<cn;
}signed main() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);solve();return 0;
}
2.E-小红的子集取反_牛客周赛 Round 31 (nowcoder.com)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int inf = 0x3f3f3f3f;
#define int long long
int f[220];
void solve() {int n,m,cn=0;cin>>n;for(int i=0;i<n;i++){cin>>f[i];cn+=f[i];}if(cn%2==0){sort(f,f+n,greater<int>());int l=cn/2,cn2=0,cn3=0;for(int i=0;i<n;i++){cn2+=f[i];cn3++;if(cn2>=l)break;}cout<<cn3;}else {cout<<-1;}
}signed main() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);solve();return 0;
}