生活随笔
收集整理的這篇文章主要介紹了
牛客小白月赛37【部分题解】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- A: 經此一役小紅所向無敵【難度: 一般 / 知識點: 模擬】
- B: 擅長解密的小紅同學【難度: 中 / 知識點: 組合數學 數學期望】
- D: 比那名居的桃子【難度: 一般 / 知識點: 前綴和】
- F: 紅色和紫色【難度: 一般 / 知識點: 博弈論】
- H: 漂亮數 【難度: 一般 / 知識點: 線性篩 打表】
- I: 加減【難度: 中 / 知識點: 貪心 雙指針 前綴和】
- J: 喵【難度: 簡單 / 知識點: 字符串】
A: 經此一役小紅所向無敵【難度: 一般 / 知識點: 模擬】
計算出每一個人可以打的回合數,取一個較小的回合。
#include<bits/stdc++.h>
using namespace std
;
long long int a
,h
,b
,k
;
int main(void)
{cin
>>a
>>h
>>b
>>k
;long long int sum
=0;int s1
=h
/b
,s2
=k
/a
;if(h
%b
) s1
++;if(k
%a
) s2
++;sum
=min(s1
,s2
)*(a
+b
);if(s1
>s2
) sum
+=a
*10;if(s1
<s2
) sum
+=b
*10;cout
<<sum
;return 0;
}
B: 擅長解密的小紅同學【難度: 中 / 知識點: 組合數學 數學期望】
#include<bits/stdc++.h>
using namespace std
;
const int N
=1e7+10;
const int mod
=1e9+7;
typedef long long int LL
;
int a
[15];
LL f
[N
];
LL
quick_mi(LL a
,LL b
,LL p
)
{LL sum
=1;while(b
){if(b
&1) sum
=(sum
*a
)%p
;b
=b
>>1;a
=(a
*a
)%p
;}return sum
%p
;
}
int main(void)
{LL sum1
=0,sum2
=1;for(int i
=0;i
<10;i
++) cin
>>a
[i
],sum1
+=a
[i
];f
[0]=1;for(int i
=1;i
<N
;i
++) f
[i
]=(f
[i
-1]*i
)%mod
;for(int i
=0;i
<10;i
++) if(a
[i
]) sum2
=(sum2
*f
[a
[i
]])%mod
;cout
<<(f
[sum1
]*quick_mi(sum2
,mod
-2,mod
))%mod
<<endl
;return 0;
}
D: 比那名居的桃子【難度: 一般 / 知識點: 前綴和】
說白了就是在長度為n的數組里找一個長度為k的區間。然后要滿足題目給的條件。
直接前綴和搞一下即可。
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
const int N
=1e5+10;
LL a
[N
],b
[N
],s
[N
],ss
[N
],n
,k
;
int main(void)
{cin
>>n
>>k
;for(int i
=1;i
<=n
;i
++) cin
>>a
[i
],s
[i
]=s
[i
-1]+a
[i
];for(int i
=1;i
<=n
;i
++) cin
>>b
[i
],ss
[i
]=ss
[i
-1]+b
[i
];LL ans1
=0,ans2
=1e18;int ans
=0;for(int i
=k
;i
<=n
;i
++){LL temp1
=s
[i
]-s
[i
-k
];LL temp2
=ss
[i
]-ss
[i
-k
];if(temp1
>ans1
){ans1
=temp1
,ans2
=temp2
;ans
=i
-k
+1;}else if(temp1
==ans1
&&temp2
<ans2
){ans2
=temp2
;ans
=i
-k
+1;}}cout
<<ans
;return 0;
}
F: 紅色和紫色【難度: 一般 / 知識點: 博弈論】
博弈論的題,不擅長,還好這個模型比較簡單。
我們可以分析出,誰最后放誰就贏了。
故如果格子數是奇數,一號手贏,否則二號手贏。
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
long long int a
,b
;
int main(void)
{cin
>>a
>>b
;if((a
*b
)&1) puts("akai");else puts("yukari");return 0;
}
H: 漂亮數 【難度: 一般 / 知識點: 線性篩 打表】
#include<bits/stdc++.h>
using namespace std
;
const int N
=1e8+10;
const int M
=1e6*5+10;
int prime
[M
],cnt
,st
[N
];
int s
[N
];
void f()
{int n
=1e8;for(int i
=2;i
<=n
;i
++){if(!st
[i
]) prime
[cnt
++]=i
;for(int j
=0;prime
[j
]<=n
/i
;j
++){st
[prime
[j
]*i
]=1;if(i
%prime
[j
]==0) break;}}for(int i
=0;prime
[i
]*prime
[i
]<=n
;i
++){for(int j
=i
;prime
[i
]*prime
[j
]<=n
;j
++){s
[prime
[i
]*prime
[j
]]++;}}for(int i
=1;i
<=n
;i
++) s
[i
]+=s
[i
-1];
}
int main(void)
{int t
; cin
>>t
;f();while(t
--){int l
,r
; scanf("%d%d",&l
,&r
);printf("%d\n",s
[r
]-s
[l
-1]);}return 0;
}
I: 加減【難度: 中 / 知識點: 貪心 雙指針 前綴和】
滑動窗口。
#include<bits/stdc++.h>
using namespace std
;
typedef long long int LL
;
const int N
=1e5+10;
LL a
[N
],n
,k
;
LL s
[N
];
int ans
=0;
int main(void)
{cin
>>n
>>k
;for(int i
=1;i
<=n
;i
++) cin
>>a
[i
];sort(a
+1,a
+n
+1);for(int i
=1;i
<=n
;i
++) s
[i
]=s
[i
-1]+a
[i
];for(int i
=1,j
=1;i
<=n
;i
++){LL temp
=a
[(i
+j
+1)/2];LL index
=(i
+j
+1)/2;while(j
<=n
&& (temp
*(index
-1-i
+1)-(s
[index
-1]-s
[i
-1])+s
[j
]-s
[index
-1]-(j
-index
+1)*temp
<=k
) ) {j
++,temp
=a
[(j
+i
+1)/2];index
=(i
+j
+1)/2;}ans
=max(ans
,j
-i
);}cout
<<ans
;return 0;
}
J: 喵【難度: 簡單 / 知識點: 字符串】
題目保證了" nya " 一定在末尾 故直接刪除后三個字符即可。
#include<bits/stdc++.h>
using namespace std
;
int main(void)
{string s
; getline(cin
,s
);while(s
.find("nya")!=-1){int t
=s
.find("nya");s
=s
.substr(0,t
)+s
.substr(t
+3);}cout
<<s
<<endl
;return 0;
}
總結
以上是生活随笔為你收集整理的牛客小白月赛37【部分题解】的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。