可化繁为简的思维题

1.Not Sitting

描述:

Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a n×mn×m grid. The seat in row rr and column cc is denoted by (r,c)(r,c), and the distance between two seats (a,b)(a,b) and (c,d)(c,d) is |ac|+|bd||a−c|+|b−d|.

As the class president, Tina has access to exactly kk buckets of pink paint. The following process occurs.

  • First, Tina chooses exactly kk seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat.
  • After Tina has painted kk seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink.
  • After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul.

Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!

Now, Rahul wonders for k=0,1,,nm1k=0,1,…,n⋅m−1, if Tina has kk buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!

输入:

The input consists of multiple test cases. The first line contains an integer tt (1t51041≤t≤5⋅104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn, mm (2nm1052≤n⋅m≤105) — the number of rows and columns of seats in the classroom.

The sum of nmn⋅m across all test cases does not exceed 105105.

输出:

For each test case, output nmn⋅m ordered integers — the distance between Rahul and Tina if both of them act optimally for every k[0,nm1]k∈[0,n⋅m−1].

样例输入:

2
4 3
1 2

样例输出:

3 3 4 4 4 4 4 4 5 5 5 5 
1 1 
大意就是在以个n*m的教室内,每一个桌子是一个単位格,有两个人(R,T)选座位,R想坐在离T更可能近的地方,T则想坐在离R更远的地方,T有0--n*m-1瓶油漆,如果被涂有油漆的位子,R是不能坐的,输出
T使用0--n*m-1瓶油漆R与T能够坐到最近距离;

一开始我是顺着这道题的思路去一个一个画图推理,弄了一个小时。关键是最后代码写出来还是错的;
其实后来发现没这么麻烦;

 T想要离R更远,所以T只能选择4个角落

 R想要与T离得更近,所以R最优的选择是选中间

因为如果R选某个靠边上的话,T就可以选到相对R的地方,会离得更远

所以为了防止R与T离得更近, T会优先去用油漆涂中间的地方。既然双方均用最优解法,我只要枚举每个格子离4个顶点最远的距离,然后再从小到大,排列一下就是答案了

#include<bits/stdc++.h>
#include<cstring>
#include<cmath>
using namespace std;
int maxm[100005];
int main()
{
    int n;
    cin>>n;
    while (n--)
    {
        int r,c,cnt=0;
        cin>>r>>c;
        memset(maxm,0,sizeof(maxm));
        for (int i=1;i<=r;i++)
        {
            for (int j=1;j<=c;j++)
            {
                int d1=abs(1-i)+abs(1-j);
                int d2=abs(1-i)+abs(c-j);
                int d3=abs(r-i)+abs(1-j);
                int d4=abs(r-i)+abs(c-j);
                maxm[cnt++]=max( max(d1,d2), max(d3,d4) );
            }
        }
        sort(maxm,maxm+cnt);
        for (int i=0;i<=r*c-1;i++)
        {
            cout<<maxm[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

 

 

GCD Arrays

 2000ms  262144K

描述:

Consider the array aa composed of all the integers in the range [l,r][l,r]. For example, if l=3l=3 and r=7r=7, then a=[3,4,5,6,7]a=[3,4,5,6,7].

Given ll, rr, and kk, is it possible for gcd(a)gcd(a) to be greater than 11 after doing the following operation at most kk times?

  • Choose 22 numbers from aa.
  • Permanently remove one occurrence of each of them from the array.
  • Insert their product back into aa.

gcd(b)gcd(b) denotes the greatest common divisor (GCD) of the integers in bb.

输入:

The first line of the input contains a single integer tt (1t1051≤t≤105) — the number of test cases. The description of test cases follows.

The input for each test case consists of a single line containing 33 non-negative integers ll, rr, and kk (1lr109,0krl1≤l≤r≤109,0≤k≤r−l).

输出:

For each test case, print "YES" if it is possible to have the GCD of the corresponding array greater than 11 by performing at most kk operations, and "NO" otherwise (case insensitive).

样例输入:

9
1 1 0
3 5 1
13 13 0
4 4 0
3 7 4
4 10 3
2 4 0
1 7 3
1 5 3

样例输出:

NO
NO
YES
YES
YES
YES
NO
NO
YES

注释:

For the first test case, a=[1]a=[1], so the answer is "NO", since the only element in the array is 11.

For the second test case the array is a=[3,4,5]a=[3,4,5] and we have 11 operation. After the first operation the array can change to: [3,20][3,20], [4,15][4,15] or [5,12][5,12] all of which having their greatest common divisor equal to 11 so the answer is "NO".

For the third test case, a=[13]a=[13], so the answer is "YES", since the only element in the array is 1313.

For the fourth test case, a=[4]a=[4], so the answer is "YES", since the only element in the array is 44.

 


这道题看起来很复杂

但其实首先知道唯一分解定理:算术基本定理可表述为:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积N=P1a1P2a2P3a3......Pnan,这里P1<P2<P3......<Pn均为质数,其中指数ai是正整数。这样的分解称为 的标准分解式。最早证明是由欧几里得给出的,由陈述证明。此定理可推广至更一般的交换代数代数数论


则我们就知道,每一个偶数它都有一个2作为质因子,可以说2是在一个集合中最多的质因子,所以我们就可以选择让偶数与奇数相乘。这样就可以以后的集合中都有2这个质因子

可能你会在想有没有更好的方法,就是不是2作为质因子,其实是不可能的,假设二不作为最后集合的质因子,假设K是,但想一想K绝对没有2在原来集合中那么多,他花的次数一定会比偶数与奇数相乘的次数多。


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        int l, r, k;
        cin >> l >> r >> k;
        int len = r - l + 1;
        int num = len >> 1;
        if ( (len&1==1) && (l&1==1) ) num++;
        if ( len==1) 
        {
            if (l==1) cout<<"NO"<<endl;
            else cout<<"YES"<<endl;
        }
        else
        {
            if (k>=num) cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    return 0;
}

  

 
posted @ 2022-01-31 16:49  次林梦叶  阅读(38)  评论(0编辑  收藏  举报