A - Factory :每天结束后 a = a + a % m;然后每次对a进行判断即可
#include<stdio.h>
#include<string.h>
#define N 100005
int c[N];
int main()
{
int a, m;
while(~scanf("%d%d", &a, &m))
{
memset( c, 0, sizeof( c ));
bool f = 0;
while(1)
{
a = a + a % m;
a %= m;
if ( a == 0 )
{
f = 1;
break;
}
else if(c[a])
{
break;
}
else
c[a] = 1;
}
printf("%s\n", f? "Yes": "No");
}
return 0;
}
B: Valuable Resources :给出n个点,求覆盖这n个点的最小正方形,其中正方形的边与坐标轴平行
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef __int64 ll;
int main()
{
ll x, xx, y, yy;
int n;
while(~scanf("%d", &n))
{
ll q, w;
scanf("%I64d%I64d", &q, &w);
x = xx = q;
y = yy = w;
n--;
while(n--)
{
scanf("%I64d%I64d", &q, &w);
x = min(q, x);
xx = max(q, xx);
y = min(y, w);
yy = max(w, yy);
}
printf("%I64d\n", max(xx - x, yy - y) * max(xx - x, yy - y));
}
return 0;
}
C - Bits:求l ~ r里面二进制中1最多的数,且尽可能小。
从l开始,每次将二进制里为0的变为1,看看有没有超出范围, (>r)超出的话直接输出即可。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef __int64 ll;
int c[100];
ll pow(ll x, ll y)
{
ll w = 1;
if( y == 0 )
return 1;
while(y--)
w *= x;
return w;
}
int main()
{
ll l, r;
int n;
while(~scanf("%d", &n))
{
while(n--)
{
scanf("%I64d%I64d", &l, &r);
ll tmp = l;
memset( c, 0, sizeof(c) );
int cnt = 0;
while(tmp)
{
c[++cnt] = tmp % 2;
tmp /= 2;
}
ll ans = l;
int pos = 1;
while ( ans <= r )
{
if( c[pos] == 0 )
{
if( ans + (ll)pow(2, pos-1 ) > r )
{
printf("%I64d\n", ans);
break;
}
else
{
ans = ans + pow(2, pos-1 );
}
}
pos ++;
}
}
}
return 0;
}