T1
这道入门题很简单,快速幂加中间判断,但是忘了开 unsigned long long,导致只拿了 70 分。
#include <iostream>
using namespace std;
#define INF 1e9
int main()
{
unsigned long long a, b;
cin >> a >> b;
unsigned long long ans = 1;
while (b)
{
if (b & 1)
{
ans *= a;
}
a *= a;
b >>= 1;
if (ans > INF)
{
cout << "-1\n";
return 0;
}
}
cout << ans << endl;
return 0;
}T2
第一眼,枚举,第二眼,推公式。AC!
#include <iostream>
#include <cmath>
#define int long long
using namespace std;
main()
{
int k;
cin >> k;
while (k--)
{
int n, d, e;
cin >> n >> d >> e;
int m = n - e * d + 2;
if (m * m < 4 * n)
{
cout << "NO\n";
continue;
}
int p = (m - sqrt(m * m - 4 * n)) / 2;
int q = (m + sqrt(m * m - 4 * n)) / 2;
if (p * q == n && p + q == m && p > 0)
{
cout << p << " " << q << endl;
}
else
{
cout << "NO\n";
}
}
return 0;
}T3
先把中缀表达式变成后缀表达式,就能计算出答案,但那两个短路死活算不出来,最后时间不够了,直接上交,RE不知道为什么不是 WA,0 分。
#include <iostream>
#include <string>
#include <stack>
using namespace std;
string s;
stack<char> st;
stack<char> st2;
stack<char> st3;
int cnt1 = 0, cnt2 = 0;
int ans = 0;
void buildStack()
{
while (char ch = getchar())
{
if (ch == '\n')
{
break;
}
switch (ch)
{
case '0':
while (!st2.empty() && st2.top() == '&' || st2.top() == '|')
{
st.push(st2.top());
st2.pop();
}
st2.push('0');
break;
case '1':
while (!st2.empty() && st2.top() == '&' || st2.top() == '|')
{
st.push(st2.top());
st2.pop();
}
st2.push('1');
break;
case '&':
st2.push('&');
break;
case '|':
while (!st2.empty() && st2.top() == '&')
{
st.push(st2.top());
st2.pop();
}
st2.push('|');
break;
case '(':
while (!st2.empty())
{
st.push(st2.top());
st2.pop();
}
st2.push('(');
break;
case ')':
while (!st2.empty() && st2.top() != '(')
{
st.push(st2.top());
st2.pop();
}
if (st2.top() == '(')
{
st2.pop();
}
break;
}
}
while (!st2.empty())
{
st.push(st2.top());
st2.pop();
}
stack<char> tmp = st;
st = stack<char>();
while (!tmp.empty())
{
st.push(tmp.top());
tmp.pop();
}
}
void getAns()
{
stack<char> tmp;
stack<char> tmp2;
while (!st.empty())
{
char n = st.top();
st.pop();
if (n == '&' || n == '|')
{
tmp.push(n);
}
else
{
tmp2.push(n);
}
while (!tmp.empty() && tmp2.size() >= 2)
{
char n1 = tmp.top();
tmp.pop();
int n2 = tmp2.top() - '0';
tmp2.pop();
int n3 = tmp2.top() - '0';
tmp2.pop();
if (n1 == '&')
{
if (n3 == 0)
{
++cnt1;
}
tmp2.push((n2 & n3) + '0');
}
else
{
if (n3 == 1)
{
++cnt2;
}
tmp2.push((n2 | n3) + '0');
}
}
}
ans = tmp2.top() - '0';
}
int main()
{
buildStack();
getAns();
cout << ans << endl << cnt1 << " " << cnt2 << endl;
return 0;
}T4
由于 T3 做了太长时间,最后十分钟直接 dfs 骗了 15 分居然还有 15 分。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int ans = 0;
struct Coord
{
int x, y;
};
bool cmp(Coord a, Coord b)
{
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
int dis(Coord a, Coord b)
{
return abs(a.x - b.x) + abs(a.y - b.y);
}
void dfs(Coord c, int num, int k, vector<vector<bool>> a)
{
if (k < 0)
{
ans = max(ans, num);
return;
}
dfs({ c.x + 1, c.y }, num + 1, k - !a[c.x + 1][c.y], a);
dfs({ c.x, c.y + 1 }, num + 1, k - !a[c.x][c.y + 1], a);
}
int main()
{
int n, k;
cin >> n >> k;
vector<vector<bool>> a(2005, vector<bool>(2005, false));
for (int i = 0; i < n; ++i)
{
int x, y;
cin >> x >> y;
a[x][y] = true;
}
for (int i = 0; i < 2005; ++i)
{
for (int j = 0; j < 2005; ++j)
{
if (a[i][j])
{
dfs({ i, j }, 0, k, a);
}
}
}
cout << ans << endl;
return 0;
}