A:
思路:
枚举一下 l l l的倍数查看是否存在有解区间即可,当然 l ∗ k < = r l*k<=r l∗k<=r
参考代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <math.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;
const int maxn = 3e5 + 5;
map<ll, ll> mp;
int pre[maxn], vis[30];
char dp[maxn];
vector<ll> vec;
typedef pair<char, int> p;
stack<p> q;
ll a[maxn], sum[maxn];
char pc[maxn];
char s[maxn];
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return (a * b) / gcd(a, b);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
//cout << gcd(2, 4) << endl;
int t;
cin >> t;
while (t--)
{
ll n, m, k = 2, l, r, ql, qr;
ql = qr = -1;
cin >> l >> r;
while (l * k <= r)
{
if (lcm(l, l * k) <= r)
{
ql = l, qr = k * l;
break;
}
}
cout << ql << ' ' << qr << endl;
}
}
B:
z z z的范围在 m i n ( 5 , k ) min(5,k) min(5,k),显然可以进行 d p dp dp
思路:
找到当前点到往回走 j ( 1 − > z ) j(1->z) j(1−>z)次的动态转移方程,如果当前走的步数等于k时更新答案即可。
参考代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <math.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;
const int maxn = 1e5 + 5;
map<ll, ll> mp;
int pre[maxn], vis[30];
//char dp[maxn];
vector<ll> vec;
typedef pair<char, int> p;
stack<p> q;
int a[maxn], sum[maxn];
char pc[maxn];
char s[maxn];
int dp[maxn][10];
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return (a * b) / gcd(a, b);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
//cout << gcd(2, 4) << endl;
int t;
cin >> t;
while (t--)
{
memset(dp, 0, sizeof dp);
int n, k, z, ans = 0;
cin >> n >> k >> z;
for (int i = 1; i <= n; i++)
cin >> a[i];
dp[1][0] = a[1];
for (int i = 2; i <= n; i++)
{
dp[i][0] = dp[i - 1][0] + a[i];
if (i - 1 == k)
ans = max(dp[i][0], ans);
for (int j = 1; j <= z; j++)
{
for (int p = 0; p <= j; p++)
{
dp[i][j] = max(dp[i][j], dp[i - 1][j - p] + (a[i] + a[i - 1]) * p + a[i]);
if (i - 1 + j * 2 == k)
ans = max(ans, dp[i][j]);
if (i + j * 2 - 2 == k)
ans = max(ans, dp[i][j] - a[i]);
}
}
}
cout << ans << endl;
}
}
C:
这题显然比B题要简单很多
思路:
显然可以知道符合条件的循环节是两个数字组成的,呢么我们直接暴力即可
证明一下时间复杂度:
注意需要考虑相同字符的情况
参考代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <math.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;
const int maxn = 1e5 + 5;
map<string, int> mp;
int pre[maxn], vis[30];
//char dp[maxn];
vector<ll> vec;
typedef pair<char, int> p;
stack<p> q;
// int a[maxn], sum[maxn];
// char pc[maxn];
// char s[maxn];
int dp[maxn][10], cnt;
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return (a * b) / gcd(a, b);
}
string cs[N];
void init()
{
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
cs[cnt] += (i + '0'), cs[cnt++] += (j + '0');
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
init();
// for (int i = 0; i < cnt; i++)
// cout << cs[i] << endl;
cin >> t;
while (t--)
{
mp.clear();
string s;
cin >> s;
int n = s.size(), k1, k2;
k1 = k2 = 0;
for (int i = 0; i < cnt; i++)
{
k1 = k2 = 0;
for (int j = 0; j < n; j++)
{
if (cs[i][0] == s[j] && !k1)
k1 = 1;
if (cs[i][1] == s[j] && k1)
k2 = 1;
if (k1 && k2)
mp[cs[i]]++, k1 = k2 = 0;
}
}
int ans = 0;
for (int i = 0; i < cnt; i++)
{
if (cs[i][0] == cs[i][1])
ans = max(ans, mp[cs[i]]);
else
ans = max(ans, mp[cs[i]] * 2);
}
cout << n - ans << endl;
}
}