A:题目链接:Problem - A - Codeforces
input:
6
1
2
3
4
5
100000000
output:
1
4
7
10
11
266666666
题意:给定范围1 <= a, b <= n,找出所有a, b,使lcd(a, b) / gcd (a, b) <= 3
策略:需要一定的思维转换能力,今天在打这场之前刚好做了一些有关gcd和lcm的题((16条消息) P1072 [NOIP2009 提高组] Hankson 的趣味题_sakura7776的博客-CSDN博客),对于里面的一些结论想的比较迅速。
我们可以知道:gcd(x, y) == z,则z一定是x和y的因子,所以该式可以化为 gcd(x / z, y / z) == 1
若lcm(x, y) == k,则lcm(x / z, y / z) == k/z
我们仅需构造gcd == 1且 lcm <= 3 的情况,然后每次将构造出的a, b倍加,并保证a, b都不超过n即可
符合条件的情况只有五种:{1,1},{1,2},{2,1},{1,3},{3,1}
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t,n;
int main()
{
ios::sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>n;
cout<<n+(n/2)*2+(n/3)*2<<endl;
}
return 0;
}
B 题目链接:Problem - B - Codeforces
input:
3
3 3 3 2
2 1 1 2
6 3 4 2
otuput:
X..
..X
.X.
XX
XX
.X..X.
X..X..
..X..X
.X..X.
X..X..
..X..X
题意:给定一个n*n个格子的正方形,初始全部为'.',并给一对x, y,表示将第x行第y列格子标记为'X',给定一个k,试向正方形中添加最少数量的'X',使得整个正方形每一个1*k以及k*1的区域都至少存在一个‘X’
策略:思路倒是非常的简单,就是以给的那个坐标为准,过这个点划和对角线平行的线,然后每间隔k-1个格子划和对角线平行的线(线过的格子变为‘X',描述的有点抽象,具体见第三组样例),但是怎么完成这个要求是个问题。研究一段时间以后发现了一个比较好的策略,可以发现同一条从左下向右上的对角线的平行线,行列和相等,若间隔为k-1,则两条线上的行列和对k取模相等。这么一来划线就非常简单啦。
具体见代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e2+5;
int t,n,k,x,y;
char c[N][N];
int main()
{
ios::sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>n>>k>>x>>y;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
c[i][j]='.';
}
}
c[x][y]='X';
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if((i+j)%k==(x+y)%k)
{
c[i][j]='X';
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<c[i][j];
}
cout<<endl;
}
}
return 0;
}
C 题目链接:Problem - C - Codeforces
input:
5
3
1 2 5
1 2 5
2
2 2
1 3
4
3 4 1 2
6 4 2 5
3
2 4 1
4 5 3
5
1 2 3 4 5
6 5 6 7 6
output:
YES
NO
NO
NO
YES
题意:给定长度为n的数组a和b,可以对a中元素进行如下操作:若 ai <= ai+1 ,可以使 ai ++,an的右边是a1(即成环型),操作次数不限,试问能否将a数组变为b数组。
策略:首先,对于每一个a中的元素,进行操作后只会比当前大,所以对于每一个ai,必须满足 ai <= bi。 其次,若 ai == bi,则ai不能再进行操作。 最后,对于每一个ai,其能达到的最大值为bi+1 + 1,所以,若当前 ai != bi,则必须满足 bi <= bi+1 + 1
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int t,n;
ll a[N],b[N];
int main()
{
ios::sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
bool flag=1;
b[0]=b[n],b[n+1]=b[1];
for(int i=1;i<=n;i++)
{
if(a[i]==b[i])
continue;
if(b[i]>b[i+1]+1||a[i]>b[i])
{
flag=0;
break;
}
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}