Problem A. Can Shahhoud Solve it?(水题)
#include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; typedef long long ll; int main() { int T; cin>>T; int s,d; while(T--){ cin>>s>>d; if(s>=d) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
Problem L. Odd and Even Count(水题)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 4e5+5; int main() { int t; cin >> t; while(t--) { int n; cin >> n; cout << n/2+n%2 << " " << n / 2 << endl; } return 0; }
Problem B. Defeat the Monsters(和数分解)
#include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; typedef long long ll; ll prime[maxn+1]; ll a[maxn],b[maxn]; void getprime(){ memset(prime,0,sizeof(prime)); for(int i=2;i<=maxn;i++){ if(!prime[i])prime[++prime[0]]=i; for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++){ prime[prime[j]*i]=1; if(i%prime[j]==0)break; } } } int fatcnt; void getfactor(ll x){ ll tmp=x; for(int i=1;prime[i]<=tmp/prime[i];i++){ if(tmp%prime[i]==0){ b[prime[i]]++; //cout<<prime[i]<<endl; while(tmp%prime[i]==0){ tmp/=prime[i]; } } } if(tmp!=0) b[tmp]++; } int main() { //ios::sync_with_stdio(false); getprime(); int T; scanf("%d",&T); int n; while(T--){ scanf("%d",&n); ll maxx=0; memset(b,0,sizeof(b)); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); maxx=max(maxx,a[i]); } for(int i=2;i<=maxx;i++)b[i]=0; for(int i=1;i<=n;i++)getfactor(a[i]); ll ans=0; for(int i=2;i<=maxx;i++){ if(b[i]){ if(b[i]%3==0)ans+=b[i]/3; else ans+=b[i]/3+1; //cout<<i<<' '<<b[i]<<endl; } } printf("%lld\n",ans); } return 0; }
Problem C. UCL Game Night(二维前缀和+暴力)
#include <bits/stdc++.h> using namespace std; const int maxn=120; int g[maxn][maxn]; int pre[maxn][maxn]; int n,m,k; void getpre(){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+g[i][j]; } } } int cal(int x1,int y1,int x2,int y2){ return pre[x2][y2]-pre[x1-1][y2]-pre[x2][y1-1]+pre[x1-1][y1-1]; } int js(int x1,int y1,int x2,int y2){ return abs(x2-x1+1)*abs(y2-y1+1); } int main(){ int T; cin>>T; while(T--){ cin>>n>>m>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>g[i][j]; } } getpre(); int ans=-1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ for(int ii=i;ii<=n;ii++){ for(int jj=j;jj<=m;jj++){ if(cal(i,j,ii,jj)<=k){ ans=max(ans,js(i,j,ii,jj)); } } } } } cout<<ans<<endl; } return 0; }
Problem F. Nim Cheater(暴力,全都xor一遍)
#include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; typedef long long ll; int a[maxn]; int main() { //ios::sync_with_stdio(false); int T; cin>>T; while(T--){ int n; cin>>n; int sum=0; for(int i=0;i<n;i++){ cin>>a[i]; if(i==0)sum=a[i]; else sum=sum^a[i]; } if(sum==0)cout<<1<<endl; else cout<<0<<endl; } return 0; }
Problem H. Don’t Ever Ask a Girl for her Codeforces Account
深搜,把A节点出发,遍历所有节点可以往下走到的方案数,然后再从b节点出发,只往父节点走,然后A的方案数=B的父节点方案数-B走到当前节点的方案数,总的方案数就是二者之积。
#include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; typedef long long ll; vector<int>g[maxn]; ll pre[maxn],par[maxn]; ll ans; int n,a,b; void dfs(int s,int p){ pre[s]=1; par[s]=p; for(auto it:g[s]){ if(it==p)continue; dfs(it,s); pre[s]+=pre[it]; } } void ans_dfs(int s){ if(s==a)return; ans+=(pre[par[s]]-pre[s])*pre[s]; ans_dfs(par[s]); } int main(){ int T; cin>>T; while(T--){ cin>>n>>a>>b; for(int i=0;i<=n;i++){ g[i].clear(); par[i]=0; pre[i]=0; } for(int i=1;i<=n-1;i++){ int u,v; cin>>u>>v; g[u].push_back(v); g[v].push_back(u); } ans=0; dfs(a,0);//处理从a出发,所有节点最多能走到的方案 ans_dfs(b);//从b出发,到所有父亲节点,计算方案数; cout<<ans<<endl; } }
Problem I. Odd and Even Queries
求区间L到R的所有乘积为奇数(或偶数)的方案数;
奇偶为奇,奇奇为奇,偶偶为偶;
所以分别记录一下奇的前缀个数和,偶的前缀个数和;
假设n为奇数的个数前缀和,m为偶数的个数前缀和
偶数的情况是2^n-1,奇数的情况是(2^n-1)*(2^m-1)+(2^m-1);
#include <bits/stdc++.h> using namespace std; const int maxn=1e6+10; const int mod=1e9+7; typedef long long ll; ll a[maxn]; ll e[maxn],o[maxn]; ll qpow(ll a,ll b){ ll ans=1; a%=mod; while(b){ if(b&1)ans=ans*a%mod; a=a*a%mod; b>>=1; } return ans; } int main() { ios::sync_with_stdio(false); int T; cin>>T; while(T--){ int n,q; cin>>n>>q; for(int i=1;i<=n;i++){ cin>>a[i]; if(a[i]%2)a[i]=1; else a[i]=0; } for(int i=1;i<=n;i++){ if(a[i]==1)o[i]=o[i-1]+1; else o[i]=o[i-1]; if(a[i]==0)e[i]=e[i-1]+1; else e[i]=e[i-1]; } while(q--){ int l,r,oo; cin>>l>>r>>oo; int x=o[r]-o[l]; int y=e[r]-e[l]; if(a[l]==1)x++; else y++; if(oo==1){ ll ans=(qpow(2,x)%mod-1)%mod; cout<<ans<<endl; } else { cout<<((((qpow(2,x)%mod-1)%mod)*((qpow(2,y)%mod-1)%mod)%mod)+((qpow(2,y)%mod-1)%mod))%mod<<endl; } } } return 0; }