Kuroni and Impossible Calculation(快速乘)

Kuroni and Impossible Calculation(快速乘)_第1张图片

题意:

在这里插入图片描述在这里插入图片描述%m的值

思路:快速乘

#include 
#include 
#include 
#include 
#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXX 100005
#define SIS std::ios::sync_with_stdio(false)
#define ll long long
#define INF 0x3f3f3f3f
#define ld long double
#define ull unsigned long long
//#include
using namespace std;
//const int MAX =100;
const double PI = 3.14159265359;
//const int mod = 1e9 + 7;
const int N=1e7+5;


struct node
{
    int x, y;
    ll num;
    bool operator <(const node other)const
    {
        return num > other.num;

    }
};
ll fastMul(ll a, ll b, ll p) {//快速乘
	a %= p;
	ll ans2 = 0;
	while (b > 0) {
		if (b & 1)
			ans2 = (ans2 + a) % p;
		b >>= 1;
		a = (a + a) % p;
	}
	return ans2;
}

int a[N],b[N],c[N];

int main()
{
    int n;
    ll m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    ll ans=1,ans1=0;
    if(n==2)
    {
        cout<<abs(a[1]-a[2])%m<<endl;
    }
    else if(n>m)
    {
        cout<<0<<endl;
    }
    else
    {
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
        {
            ans1=abs(a[i]-a[j])%m;
            ans=fastMul(ans,ans1,m);
        }
        cout<<ans<<endl;
    }


    return 0;

}

你可能感兴趣的:(CF)