标签: codeforces
A.Game
code
```cpp
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int maxn=1000050;
const int MOD=1e9+7;
int a[maxn];
int main(int argc, char const *argv[])
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i)
{
scanf("%d", a+i);
}
sort(a,a+n);
printf("%d
", a[(n-1)/2]);
return 0;
}
```
## B.Minesweeper
code
```cpp
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100050;
const int MOD=1e9+7;
char a[150][150];
int n,m;
int dx[]={0,0,1,1,1,-1,-1,-1};
int dy[]={1,-1,-1,1,0,0,1,-1};
int f(int x,int y){
int ans=0;
for (int i = 0; i < 8; ++i)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=0&&nx=0&&ny='1'&&a[i][j]<='8'&&a[i][j]-'0'!=f(i,j))
ans=false;
if(a[i][j]=='.'&&f(i,j)!=0) ans=false;
}
}
printf("%s
", ans?"Yes":"No");
return 0;
}
```
## C.Finite or not?
问$frac{p}{q}$在$b$进制下能否表示为有限小数。如果可以表示为有限小数的话,我们一定可以通过把这个小数的小数点左移若干位使之成为正整数,也就是乘以若干个$b$之后变成正整数。那么我们至少需要给$p$乘以多少个$b$呢,因为$q$中质因子的最大次幂不超过$64$,所以只要$p imes b^{64}$能整除$q$即可。
code
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=300050;
typedef long long ll;
ll qmul(ll x,ll y,ll MOD){
x%=MOD,y%=MOD;
ll ans=(x*y-(ll)((long double)x/MOD*y+1e-3)*MOD);
ans=(ans%MOD+MOD)%MOD;
return ans;
}
ll Pow(ll x,ll n,ll MOD){
ll ans=1,base=x;
while(n){
if(n&1) ans=qmul(ans,base,MOD);
base=qmul(base,base,MOD);
n>>=1;
}
return ans;
}
int main(int argc, char const *argv[])
{
int q;
scanf("%d", &q);
while(q--){
ll p,q,b;
scanf("%lld%lld%lld", &p,&q,&b);
ll a=Pow(b,64,q);
ll ans=qmul(p,a,q);
printf("%s
", ans==0?"Finite":"Infinite");
}
return 0;
}
D.XOR-pyramid
将(n)个相同元素(x)的亦或记为(x^n).
找规律可以发现$$f(a_1,a_2,a_3)=a_1 a_2^2 a_3$$
[f(a_2,a_3,a_4)=a_2 a_3^2 a_4
]
[f(a_1,a_2,a_3,a_4)=a_1a_2^3a_3^3a4
]
可以发现其实是一个杨辉三角,进而得出
[f(a_l,a_{l+1}......,a_{r-1},a_r)=f(a_{l+1},a_{l+2}......,a_{r-1},a_r)oplus f(a_l,a_{l+1}......,a_{r-2},a_{r-1})
]
code
```cpp
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100050;
const int MOD=1e9+7;
typedef long long ll;
int a[5050],n;
int f[5050][5050];
int dp[5050][5050];
int main(int argc, char const *argv[])
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d", a+i);
}
for (int len = 1; len <= n; ++len)
{
for (int i = 1; i+len-1 <= n; ++i)
{
int j=i+len-1;
if(len==1) f[i][j]=a[i],dp[i][j]=a[i];
else f[i][j]=f[i][j-1]^f[i+1][j],dp[i][j]=max(f[i][j],max(dp[i][j-1],dp[i+1][j]));
}
}
int q;
scanf("%d", &q);
while(q--){
int l,r;
scanf("%d%d", &l,&r);
printf("%d
", dp[l][r]);
}
return 0;
}
```
E.Elevator
留坑