武漢工程大學第一屆程序設計女生賽解題報告
xzc 2020.3.8
比賽鏈接:武漢工程大學第一屆程序設計女生賽
A. Multiplication (101/861)
分析:
問x平方几次後就會>=n, 按題意模擬即可。簽到題。
代碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int T;
LL k,n;
cin>>T;
while(T--)
{
cin>>n>>k;
int ans = 0;
while(k<n)
{
k = k*k;
++ans;
}
cout<<ans<<endl;
}
return 0;
}
B. Spread (28/520)
樣例輸入:
1
5 2 2
1 3
1 2
4 5
樣例輸出:
3
分析:
n個人中有m個人確定被感染。他們之中有t對接觸。求所有存在感染風險的人數(包括那m個)。
用並查集合並所有直接和間接接觸過的人,把n個人分成若干的集合。對每個集合的總人數進行計數。m個已經確認感染的人所在集合的總人數即爲答案。注意不要多算。
代碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
int pre[N];
void init(int n) //初始化
{
for(int i=0;i<=n;++i) pre[i] = i;
}
int Find(int x)
{
return x==pre[x]?x:pre[x]=Find(pre[x]);
}
void join(int x,int y)
{
if(x>y) swap(x,y);
int fx = Find(x), fy = Find(y);
if(fx!=fy) pre[fy] = fx;
}
int main()
{
int T,n,m,t,x,y,fa;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&t);
vector<int> v;
for(int i=1;i<=m;++i)
{
scanf("%d",&x);
v.push_back(x);
}
init(n);
while(t--)
{
scanf("%d%d",&x,&y);
join(x,y);
}
int ans = 0;
unordered_map<int,int> cnt; //記錄集合人數
unordered_map<int,int> mp; //記錄集合是否有被確診的人
for(int i=1;i<=n;++i)
++cnt[Find(i)];
for(int i=0;i<m;++i)
mp[Find(v[i])] = 1;
for(auto xx:mp)
ans += cnt[xx.first];
printf("%d\n",ans);
}
return 0;
}
C. Transport (24/103)
樣例輸入:
1
3 3
...
.**
...
1 3 1 1 3 3
樣例輸出:
6
分析:
簡單bfs, n行m列的矩陣,星號不能走,點號可以走,問從指揮部到小區再到醫院的最短路成。若不可達,輸出-1。
我們可以從小區出發,bfs, 記錄第一次到指揮部和第一次到醫院的步數,相加即爲答案。
代碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1005;
char a[N][N];
bool vis[N][N];
int n,m;
struct Node{
int x,y,step;
Node(){}
Node(int xx,int yy,int s):x(xx),y(yy),step(s){}
}node;
int X1,Y1,X2,Y2,X3,Y3;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
bool ok(int x,int y)
{
return (x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]=='.'&&!vis[x][y]);
}
int bfs()
{
int ans1 = -1, ans2 = -1;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
vis[i][j] = false;
int x,y,step,xx,yy;
queue<Node> Q;
Q.push(Node(X2,Y2,0));
vis[X2][Y2] = 0;
while(!Q.empty())
{
if(ans1!=-1&&ans2!=-1) return ans1+ans2;
node = Q.front();Q.pop();
x = node.x, y = node.y, step = node.step;
for(int i=0;i<4;++i)
{
xx = x+dx[i], yy = y+dy[i];
if(!ok(xx,yy)) continue;
if(xx==X1&&yy==Y1)
{
if(ans1==-1) ans1 = step+1;
if(ans2!=-1) return ans1+ans2;
}
else if(xx==X3&&yy==Y3)
{
if(ans2==-1) ans2 = step+1;
if(ans1!=-1) return ans1+ans2;
}
vis[xx][yy] = true;
Q.push(Node(xx,yy,step+1));
}
}
return -1;
}
int main()
{
int T,step1,step2;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%s",a[i]+1);
cin>>X1>>Y1>>X2>>Y2>>X3>>Y3;
printf("%d\n",bfs());
}
return 0;
}
D. OneTrunKill (0/1)
分析: 0/1的大模擬,沒做,跳過吧
E. SSR (10/82)
樣例輸入:
2
2
4
樣例輸出:
2
2
4
分析:
恰好一半人抽到SSR的概率。總共的情況爲2^n種,我們調出一半人抽SSR的方案數爲C(n.n/2) ,概率爲C(n,n/2) / 2^n, 注意出發取模要乘以逆元
代碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6+10;
const int mod = 1E9+7;
void exgcd(LL a,LL b,LL&d,LL &x,LL&y);
LL fast_pow(LL a,LL b,LL mod);
LL getNi(LL a,LL m);
LL fac[N] = {1},rev[N];
void init()
{
for(int i=1;i<=1000000;++i)
fac[i] = fac[i-1]*i%mod;
rev[1000000] = getNi(fac[1000000],mod);
for(int i=1000000-1;i>=1;--i)
rev[i] = rev[i+1]*(i+1)%mod;
}
LL C(int n,int m)
{
if(n<m) return 0;
if(m==0||m==n) return 1;
return fac[n]*rev[m]%mod*rev[n-m]%mod;
}
int main()
{
init();
int T,n;
cin>>T;
while(T--)
{
cin>>n;
int m = n/2;
LL tot = fast_pow(2ll,n,mod);
tot = getNi(tot,mod);
LL up = C(n,m);
up = up*tot%mod;
cout<<up<<endl;
}
return 0;
}
void exgcd(LL a,LL b,LL&d,LL &x,LL&y){
if(b==0) {
d = a; x = 1; y = 0;return;
}
exgcd(b,a%b,d,y,x);
y -= (a/b)*x;
}
LL getNi(LL a,LL m)
{
LL d,x,y;
exgcd(a,m,d,x,y);
return (x%m+m)%m;
}
LL fast_pow(LL a,LL b,LL mod)
{
if(a==0) return 0;
LL ans = 1;
while(b)
{
if(b&1) ans = ans*a%mod;
a = a*a%mod;
b>>=1;
}
return ans;
}
F. 非對稱加密算法RSA (11/38)
分析:
RSA,搶了個FB。 題目很長,簡化以後,就是給一個p,q , 然後n = p*q, n的歐拉函數Fai(n) = (p-1)*(q-1), 求d關於Fai(n)的乘法逆元,並用x^e%n加密明文x 。
代碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+10;
const int mod = 1E9+7;
void exgcd(LL a,LL b,LL&d,LL &x,LL&y); //拓展歐幾里得
LL fast_pow(LL a,LL b,LL mod); //快速冪
LL getNi(LL a,LL m); //用拓展歐幾里得求逆元
int main()
{
LL p,q,x,Fain,n;
const int e = 65537;
cin>>p>>q>>x;
n = p*q; //計算n
Fain = (p-1)*(q-1); //計算Fai(n)
LL d = getNi(e,Fain); //d 是 e 關於 Fai(n)的乘法逆元
cout<<d<<endl; //輸出d
cout<<fast_pow(x,e,n)<<endl; //密文爲 x^e % n
return 0;
}
void exgcd(LL a,LL b,LL&d,LL &x,LL&y){
if(b==0) {
d = a; x = 1; y = 0;return;
}
exgcd(b,a%b,d,y,x);
y -= (a/b)*x;
}
LL getNi(LL a,LL m)
{
LL d,x,y;
exgcd(a,m,d,x,y);
return (x%m+m)%m;
}
LL fast_pow(LL a,LL b,LL mod)
{
if(a==0) return 0;
LL ans = 1;
while(b)
{
if(b&1) ans = ans*a%mod;
a = a*a%mod;
b>>=1;
}
return ans;
}
G. 網絡線路選擇 (3/28)
分析:
n個格子,每個可以塗m種色,求存在相鄰兩格同色的方案數
若n爲1,則沒有相鄰的,答案爲0
若m爲1且n>1,則只有1種方案,都塗這唯一一種顏色。
一般的情況,我們可以用所有方案減去不連色的方案。所有方案爲m^n, 不連色的方案爲:m*(m-1)^(n-1) 。注意模數很大,可能會溢出,用快乘穩妥。
代碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL fast_pow(LL a,LL b,LL mod);
LL fast_Mul(LL a,LL b,LL mod);
int main()
{
int T = 0;
LL m,n,p,mod;
cin>>T;
while(T--)
{
cin>>n>>m>>p;
mod = n*p;
if(n==1)
{
puts("0");
continue;
}
if(m==1)
{
cout<<1%mod<<endl;
continue;
}
LL tot = fast_pow(m,n,mod);
LL t = fast_pow(m-1,n-1,mod);
t = fast_Mul(t,m,mod);
tot = ((tot-t)%mod+mod)%mod;
cout<<tot<<endl;
}
return 0;
}
LL fast_pow(LL a,LL b,LL mod)
{
if(a==0) return 0;
LL ans = 1;
while(b)
{
if(b&1) ans = fast_Mul(ans,a,mod);
a = fast_Mul(a,a,mod);
b>>=1;
}
return ans;
}
LL fast_Mul(LL a,LL b,LL mod)
{
LL ans = 0;
if(a==0||b==0) return 0;
if(a<b) swap(a,b);
while(b)
{
if(b&1) ans = (ans+a)%mod;
a = (a<<1)%mod;
b>>=1;
}
return ans;
}
H. 神奇的硬幣II (3/31)
樣例輸入:
5 5
3 5 6 3 8
3 5 6 3 8
3 2 5 1 6
樣例輸出:
1
分析:
n個硬幣,m個物品,每個硬幣有價值v和能量e, 每個物品有重量w。如果硬幣價值>=物品重量,物品能量就要加上硬幣的能量。求能量最大,id最小的物品。
我們可以對硬幣進行處理,能量爲0的不影響結果,可以去掉,面值相同的硬幣可以視爲一個,把能量相加到一個裏面即可。然後我們按硬幣價值從大到小排序,求關於能量的前綴和。每個物品,我們二分出價值大於物品質量的第最小价值硬幣,前綴和即爲該物品的能量。計算能量時順便維護最大值級id即可。
代碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1E6+10;
int VV[N],EE[N];
LL sum[N];
struct Node{
int v,E;
bool operator < (const Node& rhs)const{
return v < rhs.v;
}
}node[N];
struct P{
int id,w;
P(int _id=0,int _w=0)
{
id = _id;
w = _w;
}
bool operator < (const P& rhs)const{
return w < rhs.w;
}
}pp[N];
int main()
{
int n,m,E;
cin>>n>>m;
for(int i=1;i<=n;++i)
scanf("%d",VV+i);
for(int i=1;i<=n;++i)
scanf("%d",EE+i);
int cnt = 0;
for(int i=1;i<=n;++i)
{
if(EE[i]!=0) ++cnt,node[cnt].v = VV[i],node[cnt].E =EE[i];
}
n = cnt;
sort(node+1,node+n+1);
int p = 1;
for(int i=2;i<=n;++i)
{
if(node[i].v==node[p].v)
{
node[p].E += node[i].E;
}
else
{
++p; node[p].v = node[i].v, node[p].E = node[i].E;
}
}
n = p;
for(int i=1;i<=m;++i)
scanf("%d",&pp[i].w),pp[i].id = i;
if(n==0)
{
puts("1"); return 0;
}
sort(pp+1,pp+m+1);
if(node[n].v<pp[1].w)
{
puts("1"); return 0;
}
sum[n] = node[n].E;
for(int i=n-1;i>=1;--i)
sum[i] = sum[i+1]+node[i].E;
LL Maxx = -1e17;
int id = -1;
int left = 0,right = n,mid;
for(int i=1;i<=m;++i)
{
if(pp[i].w>node[n].v)
{
if(Maxx<0||Maxx==0&&id>pp[i].id)
{
Maxx = 0, id = pp[i].id;
}
continue;
}
left = 0, right = n;
while(right-left>1)
{
mid = (left+right)>>1;
if(node[mid].v>=pp[i].w) right = mid;
else left = mid;
}
if(Maxx<sum[right]||Maxx==sum[right]&&pp[i].id<id)
{
Maxx = sum[right];
id = pp[i].id;
}
}
cout<<id<<endl;
return 0;
}
I.神奇的硬幣I (21/154)
分析:
其實就是求能買到物品的價值最小的硬幣能買到幾個。我們可以對硬幣價值排序,對物品重量排序,二分查找到能買到物品的最小价值的幣。然後看重量小於該幣值的物品有幾個,再次二分可得到答案。
代碼:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1E6+20;
int v[N],w[N];
int main()
{
int n,m;
while(cin>>n>>m)
{
for(int i=1;i<=n;++i)
scanf("%d",v+i);
for(int i=1;i<=m;++i)
scanf("%d",w+i);
sort(v+1,v+1+n);
sort(w+1,w+1+m);
if(v[n]<w[1])
{
puts("-1");
return 0;
}
int idd = lower_bound(v+1,v+1+n,w[1])-v;
int id = upper_bound(w+1,w+1+m,v[idd])-w-1;
printf("%d\n",id);
}
return 0;
}
寫完了。
2020.3.8 20:47 xzc