题目链接:Codeforces
B:题目大意:给你n个整数对,让你求是否有一个数,这个数对于每一个整数对,它是至少其中一个数的因子。
1≤n≤150000, 2≤ai,bi≤2e9
先说自己TLE的思路:选一对数,求出它们所有的因数,然后对其他的数进行核对。并且对于每一个要塞进vector的数据,检查会不会有它的因数已经在里面了。
思路是对的,可惜优化的时候没处理好。比如直接来一个2e9的数据,在处理vector的时候就超时了。
正解:首先容易知道,对于一个数,我们只需要求出它的质数约数就行了。具体操作:遇到一个约数,压入vector,再除这个数到不能整除为止。比如2 * 3 * 3 * 4 = 72这个数,第一步处理,2压入vector,再把2,4(2 *2)消了,压入一个3,再把两个3消了。这样就能得到所有的质数约数。这样我们只需要处理到sqrt(n),因为除了因子之后的数一定小于sqrt(n).
时间复杂度n * sqrt(n),加上常数2n * sqrt(n),算了一下1e8可以过。(实际比这个数小)
AC Code:
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define pb push_back
#define lowbit(x) x&(-x)
#define PII pair<int, int>
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)150000 + 5;
using namespace std;
vector< pair<ll, ll> > v;
vector<ll> p;
void work(ll x){
int m = sqrt(x);
for(int i = 2; i <= m; i++){
if(x % i == 0){
p.pb(i);
while(x % i == 0) x /= i;
}
}
if(x != 1) p.pb(x);
}
int main()
{
int n; scanf("%d", &n);
for(int i = 1; i <= n; i++){
ll x, y; scanf("%lld %lld", &x, &y);
v.pb(make_pair(x, y));
}
work(v[0].fi);
work(v[0].se);
int len = p.size();
for(int i = 0; i < len; i++){
bool flag = true;
for(int j = 0; j < n; j++){
if(v[j].fi % p[i] != 0 && v[j].se % p[i] != 0) { flag = false; break; }
}
if(flag) return 0 * printf("%lld\n", p[i]);
}
puts("-1");
return 0;
}
C:一个只包含w,b的字符串,你可以进行以下操作:选一个间隙,分别翻转两边的字符串,这样能得到一个新的字符串。
问你len的最大值。len定义:字符串中最长 连续 相邻字符不同 的子串长度
解题思路:又是差一点想到。思维题,可以等效。由于题目的特殊性,答案子串只有四种可能:前缀,后缀,被包含,前缀 +后缀。所以我们延伸这个字符串找出最长的满足条件即可。
证明:被包含不谈,这种情况很容易得到
前缀后缀也好考虑
前缀+后缀:其实在进行翻转的时候,我们把首尾相连了,又基于题目条件的特殊性,枚举每一个位置的翻转即可,然而这样会超时,所以有了延伸字符串,求最大值即可。
可惜
AC Code:
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define pb push_back
#define lowbit(x) x&(-x)
#define PII pair<int, int>
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 7;
const int maxn = (int)150000 + 5;
using namespace std;
int main()
{
string s;
cin >> s;
int now = 1, ans = 0, len = s.size();
s += s;
for(int i = 1; i < 2 * len; i++){
if(s[i] != s[i-1]) now++;
else ans = max(ans, now), now = 1;
}
ans = max(ans, now);
ans = min(ans, len);
cout << ans << endl;
return 0;
}
最近打的比赛都好不舒服,每次都有一个甚至几个题差一点点(比一点都少),难受受,还是多做题吧。