2021牛客暑期多校训练营8
A-Ares, Toilet Ares
思路
直接按照样例解释的公式算就行
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 4933;
ll qpow(ll x, ll n)
{
ll ans = 1;
while(n)
{
if(n & 1)
ans = (ans * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return ans;
}
int main()
{
ll n, m, k, a, l;
cin >> n >> m >> k >> a >> l;
ll ans = 1;
for(int i = 0; i < k; i++)
{
ll x, y, z;
cin >> x >> y >> z;
if(x == 0)
continue;
ans = ans * (z - y) % mod;
ans = ans * qpow(z, mod - 2) % mod;
}
ans = (ans + a) % mod;
cout << ans << endl;
return 0;
}
D-OR
思路
比赛的时候想到了 a + b = a ∣ b + a & b a + b = a | b + a \& b a+b=a∣b+a&b ,但是想歪了,以为 a & b a \& b a&b 会有什么可以直接运用的性质,一直在猜性质,而且想错了按位枚举的复杂度,以为是 2 31 2^{31} 231 次枚举 ,其实只需要 62 62 62 次就可以了,因为只需要该位能否是1/能否是0,既能是1又能是0答案乘2,只能是其中之一答案不变,否则答案为0。
另,写嵌套if/else一定要加括号,比如我后来一直wa的原因竟然是没括号,else被匹配到内层的if/else里了……
//我是一名精通代码的女程序员,9行代码,让我为这题看了60分钟
bool check(int now, int And, int Or)
{
if(now)
if(!Or)
return 0;
else
if(And) //这里没加括号,变成了最近的if的else if选项
return 0;
return 1;
}
代码
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
int b[N], c[N];
bool check(int now, int And, int Or) {
if (now && !Or) return 0;
else if (!now && And) return 0;
return 1;
}
int ok(int now, int i, int n)
{
for(int j = 1; j < n; j++)
{
int And = ((c[j]) >> i) & 1; ///与位
int Or = (b[j] >> i) & 1; ///或位
if(!check(now, And, Or))
return 0;
if(now)
now = And;
else
now = Or;
}
return 1;
}
int main()
{
int n;
cin >> n;
bool f = 0;
for(int i = 1; i < n; i++)
{
cin >> b[i];
}
for(int i = 1; i < n; i++)
{
cin >> c[i];
c[i] -= b[i];
}
ll res = 1;
for(int i = 0; i <= 30; i++)
{
int cnt = ok(0, i, n);
cnt += ok(1, i, n);
res = res * cnt;
}
if(f)
{
cout << 0 << endl;
}
else
{
cout << res << endl;
}
return 0;
}
E-Rise of Shadows
思路
没有哪个闰年是素数
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
int main () {
int t;
scanf("%d", &t);
while (t--) {
int x;
scanf("%d", &x);
printf("no\n");
}
return 0;
}
F-Robots
思路
分治,如果询问的起点和终点在中线两侧,处理出从中线能够到达的上侧和下侧点(用或运算),用bitset存每个位置的状态,转移方程为 b s t [ i ] [ j ] = b s t [ i − 1 ] [ j ] & b s t [ i ] [ j − 1 ] bst[i][j] = bst[i - 1][j] \& bst[i][j - 1] bst[i][j]=bst[i−1][j]&bst[i][j−1] 。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int N = 505;
const int M = 5e5;
int mp[N][N];
int sumLR[N][N], sumUD[N][N];
int ans[M];
struct node
{
int s, t, id;
};
bitset<N> LM[N][N], MR[N][N];
int n, m;
int encode(int x, int y)
{
return x * 1000 + y;
}
P decode(int x)
{
return P(x / 1000, x % 1000);
}
void check(int i, vector<node>& vec)
{
int op, x1, y1, x2, y2;
cin >> op >> x1 >> y1 >> x2 >> y2;
if(x1 > x2 || y1 > y2)
{
return ;
}
if(op == 1)
{
if(y1 == y2 &&
sumUD[x2][y2] - sumUD[x1 - 1][y1] == 0)
ans[i] = 1;
}
else if(op == 2)
{
if(x1 == x2 &&
sumLR[x2][y2] - sumLR[x1][y1 - 1] == 0)
ans[i] = 1;
}
else
{
vec.push_back({encode(x1, y1), encode(x2, y2), i});
}
}
void solve(int l, int r, vector<node>& v)
{
if(l > r)
return ;
int mid = (l + r) >> 1;
for(int i = n; i >= 1; i--)
{
LM[i][mid].reset();
if(!mp[i][mid])
{
LM[i][mid].set(i);
LM[i][mid] |= LM[i + 1][mid];
}
}
for(int i = 1; i <= n; i++)
{
MR[i][mid].reset();
if(!mp[i][mid])
{
MR[i][mid].set(i);
MR[i][mid] |= MR[i - 1][mid];
}
}
for(int j = mid - 1; j >= l; j--)
{
for(int i = n; i >= 1; i--)
{
LM[i][j].reset();
if(!mp[i][j])
{
LM[i][j] |= LM[i][j + 1];
LM[i][j] |= LM[i + 1][j];
}
}
}
for(int j = mid + 1; j <= r; j++)
{
for(int i = 1; i <= n; i++)
{
MR[i][j].reset();
if(!mp[i][j])
{
MR[i][j] |= MR[i][j - 1];
MR[i][j] |= MR[i - 1][j];
}
}
}
vector<node> lv, rv;
for(auto q : v)
{
P s = decode(q.s);
P t = decode(q.t);
int id = q.id;
if(s.second > mid)
{
rv.push_back(q);
}
else if(t.second < mid)
{
lv.push_back(q);
}
else
{
if((LM[s.first][s.second] & MR[t.first][t.second]).any() )
ans[id] = 1;
}
}
solve(l, mid - 1, lv);
solve(mid + 1, r, rv);
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
char c;
getchar();
for(int j = 1; j <= m; j++)
{
c = getchar();
mp[i][j] = c - '0';
sumLR[i][j] = sumLR[i][j - 1] + mp[i][j];
sumUD[i][j] = sumUD[i - 1][j] + mp[i][j];
}
}
int q;
cin >> q;
vector<node> vec;
for(int i = 0; i < q; i++)
{
check(i, vec);
}
solve(1, m, vec);
for(int i = 0; i < q; i++)
{
cout << ((ans[i] == 1) ? "yes" : "no") << endl;
}
return 0;
}
K-Yet Another Problem About Pi
思路
如果沿直线走 c n t cnt cnt 步,覆盖的区块数为 c n t ∗ 2 + 4 cnt * 2 + 4 cnt∗2+4 ;如果沿斜线走 c n t cnt cnt 步,覆盖的区块数为 c n t ∗ 3 + 4 cnt * 3 + 4 cnt∗3+4 。同时,先走直线最后走一段斜线,或者先走斜线最后走一段直线都有可能会得到最佳答案。把上面四种情况枚举一下,就能得到最后的答案。实践证明情况3和4只需要枚举两个就可以了,虽然我也证不出来。
比赛的时候根本没想到斜着走可能会更优,呜呜
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = acos(-1);
const double eps = 1e-8;
int dcmp(double num)
{
if(fabs(num) < eps)
return 0;
return num > 0 ? 1 : -1;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
double w, d;
scanf("%lf%lf", &w, &d);
if(w > d)
swap(w, d);
double len = sqrt(w * w + d * d);
int cnt = 0;
int ans = 0;
for(int i = 0; i <= 2; i++)
{
if(dcmp(pi - len * i) > 0)
{
cnt = (int)((pi - len * i) / w);
ans = max(ans, cnt * 2 + 4 + 3 * i);
}
if(dcmp(pi - w * i) > 0)
{
cnt = (int)((pi - w * i) / len);
ans = max(ans, cnt * 3 + 4 + 2 * i);
}
}
printf("%d\n", ans);
}
return 0;
}