Codeforces Round #641 (Div. 1)
作者:互联网
A - Orac and LCM
先对每一个数字进行质因数分解,记录,该质因数的次方和出现次数。
如果出现了n-1次,那么答案有该质因数最小次方。如果出现了n次,那么答案拥有该质因数次小次方。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 100010;
int n;
int a[N];
vector<int>vec[N], vec2[N * 2];
int mp[2 * N];
ll quick_pow(ll a, ll b)
{
ll res = 1;
while (b)
{
if (b & 1)res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int main()
{
n = read();
upd(i, 1, n)
{
a[i] = read();
int aa = 2;
while (aa*aa<=a[i])
{
int cnt = 0;
while (a[i]%aa==0)
{
cnt++;
a[i] /= aa;
}
if (cnt) {
mp[aa] ++;
vec2[aa].push_back(cnt);
}
aa++;
}
if (a[i]) {
mp[a[i]]++; vec2[a[i]].push_back(1);
}
}
ll ans = 1;
upd(i, 1, 200000)sort(vec2[i].begin(), vec2[i].end());
upd(i,1,200000)
if (mp[i])
{
if (mp[i] >= n - 1)
{
if (mp[i] == n - 1)
{
ans *= quick_pow(i*1ll, vec2[i][0]);
}
else {
ans *= quick_pow(i*1ll, vec2[i][1]);
}
}
}
cout << ans << endl;
return 0;
}
B - Orac and Medians
找规律
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 1e5 + 10;
int T, n, k;
int a[N];
vector<int>vec;
int main()
{
T = read();
while (T--)
{
n = read(), k = read();
int numx=0,numd=0,nums = 0;
vec.clear();
upd(i, 1, n)
{
a[i] = read();
vec.push_back(a[i]);
if (a[i] < k)numx++;
if (a[i] == k)nums++;
if (a[i] > k)numd++;
}
if (!nums)
{
printf("no\n"); continue;
}
sort(vec.begin(), vec.end());
int mid = (n + 1) / 2;
if (vec[mid - 1] == k)
{
printf("yes\n");
continue;
}
if (numd >= n-mid+1) {
printf("yes\n");
continue;
}
else {
bool flag = (a[n - 1] >= k && a[n] >= k);
upd(i, 1, n-2)
{
if (a[i] >= k)
{
flag |= (a[i + 1] >= k || a[i + 2] >= k);
}
}
if (!flag)printf("no\n");
else printf("yes\n");
}
}
return 0;
}
C - Orac and Game of Life
首先通过打表可以发现,当反转进行到某次开始,继续往后,他是重复交替出现的。
我们就可以思考并发现,我们将相邻的并且颜色相同的块全部连在一起,称为连通块,那么这些块从第一次开始,便会反转。
当进行第二次反转的时候,第一次连通块的边界,就将扩展,因为连通块的边界一定颜色不同,翻转一次,颜色相同,所以连通块扩展。
所以,我们可以发现,某一块的颜色的变化,是他最先进入连通块后,根据奇偶判断。我们就可以使用最短路算法求得。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 1e3 + 10;
char s[N][N];
int n, m, t;
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,1,-1 };
queue<pir>q;
ll dist[N][N];
void check(int i, int j)
{
up(o, 0, 4)
{
int xx = i + dx[o]; int yy = j + dy[o];
if (xx<1 || xx>n || yy<1 || yy>m)continue;
if (s[xx][yy] == s[i][j])
{
dist[i][j] = 0;
q.push(make_pair(i, j));
}
}
}
int main()
{
n = read(), m = read(), t = read();
upd(i, 1, n)
{
scanf("%s", s[i] + 1);
}
upd(i, 1, n)upd(j, 1, m)dist[i][j] = 1e18 + 1;
upd(i, 1, n)
{
upd(j, 1, m)
{
check(i, j);
}
}
while (q.size())
{
pir temp = q.front();
q.pop();
up(o, 0, 4)
{
int xx = temp.first + dx[o]; int yy = temp.second + dy[o];
if (xx<1 || xx>n || yy<1 || yy>m)continue;
if (dist[xx][yy] > dist[temp.first][temp.second] + 1)
dist[xx][yy] = dist[temp.first][temp.second] + 1, q.push(make_pair(xx, yy));
}
}
ll x, y, pos;
while (t--)
{
x = read(),y = read(), pos = read();
if (dist[x][y] >= pos)
{
printf("%c\n", s[x][y]);
}
else {
int aa = s[x][y] - '0';
printf("%d\n", (pos - dist[x][y]) % 2 ? aa ^ 1 : aa);
}
}
return 0;
}
标签:ch,const,int,ll,Codeforces,641,read,Div,include 来源: https://www.cnblogs.com/LORDXX/p/13033615.html