CodeForces - 1305C Kuroni and Impossible Calculation(鸽巢原理)

题目链接:点击查看

题目大意:给出 n 个数,输出两两绝对值之差相乘对 m 取模后的答案

题目分析:读完题后乍一看感觉 n 很大,不能 n * n 暴力解决,但是发现 m 非常小,因为有了 m 的限制,而操作时只涉及了加,减以及乘法运算,所以每个数字的范围也被限制到了 [ 0 , m ) 之间,并且不难发现,如果 n 个数中取模后存在两个数 a[ i ] = a[ j ] ,那么 abs( a[ i ] - a[ j ] ) % m = 0 ,所以最终的答案乘以 0 也变成了 0,再根据鸽巢原理,就可以下结论了:

  1. 当 n > m 时,此时至少存在一个 x ∈ [ 0 , m ),满足 x 的出现次数大于等于 2
  2. 当 n <= m 时,n * n 暴力计算答案就好了,时间复杂度为 O( m *m )

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;
      
typedef long long LL;
     
typedef unsigned long long ull;
      
const int inf=0x3f3f3f3f;
 
const int N=1e3+100;

int n,m;

LL a[N];

int main()
{
#ifndef ONLINE_JUDGE
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
#endif
//    ios::sync_with_stdio(false);
    scanf("%d%d",&n,&m);
    if(n>m)
    {
        puts("0");
        return 0;
    }
    for(int i=1;i<=n;i++)
        scanf("%d",a+i);
    LL ans=1;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            ans=ans*llabs(a[i]-a[j])%m;
    printf("%lld\n",ans);
    
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
}
发布了672 篇原创文章 · 获赞 26 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104650724