https://acm.ecnu.edu.cn/contest/20/
A不等式
B
https://acm.ecnu.edu.cn/problem/3303/
https://acm.ecnu.edu.cn/contest/31/
预推免
https://acm.ecnu.edu.cn/contest/31/problem/A/
A. 吉吉木的野望
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll d,k;
cin>>d>>k;
double sum = 0;
ll an,sn;
int cnt = 0;
for(ll i = min(k,ll(1e7)) ;i >= 1;i--){
an = 1 + d * (i - 1);
// cout<<an<<endl;
if(sum == 0)
sum = an;
else
sum = an + 1.0/sum;
cnt++;
}
sum =1.0/sum;
printf("%.16lf\n",sum);
return 0;
}
B. 皇后问题
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
ll visx[maxn];
ll visy[maxn];
ll k1[3*maxn],k2[3*maxn];
int main()
{
ll n,x,y;
ll cnt = 0;
cin>>n;
while(n--){
cin>>x>>y;
if(visx[x] != 0)
cnt += visx[x];
if(visy[y] != 0)
cnt += visy[y];
visx[x]++;
visy[y]++;
if(k1[y - x + maxn] != 0)
cnt += k1[y-x + maxn];
if(k2[x + y + maxn] != 0)
cnt += k2[x+y +maxn];
k1[y - x + maxn]++;
k2[x + y + maxn]++;
}
cout<<cnt<<endl;
return 0;
}
C
过90%代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
set<ll> vis;
ll slove(ll s){
ll ans = 1;
ll cnt = 0;
ll x = s;
for( ll i = 2; i <= sqrt(s);i++){
while(x % i == 0){
x /= i;
cnt++;
}
ans = ans*(cnt + 1);
cnt = 0;
}
if(x != 1) ans *= 2;
return ans;
}
int main()
{
ll q,n;
cin>>q>>n;
ll sum = 0;
if(q == 1){
sum = slove(n);
}
else{
for(ll a = 1 ;a < n; a++){
vis.clear();
ll cnt = 0;
for (ll x = 1; x <= (n - 1)/ a;x++ ){
ll by = n - a*x;
for( ll i = 1 ;i<= sqrt(by);i++){
if(by % i ==0){
if(vis.find(i) == vis.end()){
cnt++;
// cout<<a << " "<<i<<endl;
}
vis.insert(i);
if(vis.find(by/i) == vis.end()){
cnt++;
// cout<<a << " "<<by/i<<endl;
}
vis.insert(by/i);
// vis[by /i] = 1;
}
}
}
sum += cnt;
}
}
cout<<sum<<endl;
return 0;
}
D. 我认识你
超时代码1:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e4+5;
vector<int> g[maxn];
set<int> vis;
int main()
{
int n,m,u,v,q,s,t;
cin>>n>>m;
while(m--){
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
cin>>q;
while(q--){
cin>>s>>t;
vis.clear();
int cnt = 0;
for(auto c:g[s]){
vis.insert(c);
}
for(auto c:g[t]){
if(vis.find(c) != vis.end()) cnt++;
vis.insert(c);
}
cout<<cnt<<endl;
}
return 0;
}
超时代码2:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e4+5;
map<int, set<int> > g;
int main()
{
int n,m,u,v,q,s,t;
cin>>n>>m;
while(m--){
cin>>u>>v;
g[u].insert(v);
g[v].insert(u);
}
cin>>q;
while(q--){
cin>>s>>t;
int cnt = 0;
set<int>::iterator it1 = g[s].begin();
set<int>::iterator it2 = g[t].begin();
// cout<<*it1<<endl;
while (it1 != g[s].end() && it2 != g[t].end())
{
if( *it1 < *it2)
it1++;
else if( *it1 == *it2){
it1++;
it2++;
cnt++;
}
else
it2++;
}
cout<<cnt<<endl;
// cnt = g[s].size() + g[t].size() - cnt;
// cout<<cnt<<endl;
}
return 0;
}
正解:
用bitset二进制压缩优化
bitset类型在定义时就需要指定所占的空间,例如bitset<233>bit;
bitset类型可以用string和整数初始化(整数转化成对应的二进制)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e4+5;
bitset<maxn> g[maxn];
int main()
{
int n,m,u,v,q,s,t;
cin>>n>>m;
while(m--){
cin>>u>>v;
g[u][v] = 1;
g[v][u] = 1;
}
cin>>q;
while(q--){
cin>>s>>t;
int cnt = 0;
bitset <maxn> tmp = (g[s] & g[t]);
cnt = tmp.count();
cout<<cnt<<endl;
}
return 0;
}