题解 P4905 【报纸】

· · 题解

这题很简单,感觉蓝色给高了.

首先你要会判断互质.然后把所有N*N的范围内所有i和j不互质的位置都标记起来.

因为这题只是拍1*2的范围,就说明最多只有两个位置练起来一起拍照,所以就非常好判断了,只要一个一个找过去,在找的过程中判断是不是互质的,然后在判断下面一个位置是不是互质的,在判断右边一个位置是不是互质的.

先来教大家什么是互质:

两个正整数只有一个公因数1时,它们的关系叫做互质,如3和11互质

上代码

bool asd(int a,int b)
{
    if(a<=0||b<=0||a==b)
    {
        return false;
    }
    else if(a==1||b==1)
    {
        return true;
    }
    else
    {
        while(1)
        {   
            int t=a%b;
            if(t==0)
            {
                break;
            }
            else
            {
                a=b;
                b=t;
            }
        }
        if(b>1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

只要把不是互质的标记起来就可以了,注意i=1和j=1时比较特殊,所以不算的,不要标记起来

上代码

for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==1&&j==1)
            {
                continue;
            }
            if(!asd(i,j))
            {
                f[i][j]=1;
            }
        }
    }

最后再循环判断就可以了

上代码

for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(f[i][j]&&!b[i][j])
            {
                ans++;
                if(f[i+1][j]&&!b[i+1][j])
                {
                    b[i+1][j]=1;
                }
                if(f[i][j+1]&&!b[i][j+1])
                {
                    b[i][j+1]=1;
                }
            }
        }
    }

最后要注意一点,一个位置是可以多次拍照的,所以else不要乱用,不然容易出错.

最终结果:

#include <bits/stdc++.h>
//#pragma GCC optimize(2)//O2优化
using namespace std;
typedef unsigned long long ll;
const ll N=3000+10;
int n;
int ans,f[N][N];
bool b[N][N];
bool asd(int a,int b)
{
    if(a<=0||b<=0||a==b)
    {
        return false;
    }
    else if(a==1||b==1)
    {
        return true;
    }
    else
    {
        while(1)
        {   
            int t=a%b;
            if(t==0)
            {
                break;
            }
            else
            {
                a=b;
                b=t;
            }
        }
        if(b>1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==1&&j==1)
            {
                continue;
            }
            if(!asd(i,j))
            {
                f[i][j]=1;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(f[i][j]&&!b[i][j])
            {
                ans++;
                if(f[i+1][j]&&!b[i+1][j])
                {
                    b[i+1][j]=1;
                }
                if(f[i][j+1]&&!b[i][j+1])
                {
                    b[i][j+1]=1;
                }
            }
        }
    }
    cout<<ans;
    return 0;
}