Блог пользователя AM51

Автор AM51, 11 лет назад, По-английски

http://codeforces.net/contest/272/problem/D

can somebody point out mistake in my code getting WA for case 2 1 2 2 2 4

#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pii pair<int,int>
#define pb(x) push_back(x)
#define S(x) scanf("%d",&x)
#define Sl(x) scanf("%lld",&x)
#define M(x,i) memset(x,i,sizeof(x))
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
using namespace std;
map<int,int> mp;
int a[100005];
int b[100005];
ll fact[200005];
int MOD=1;
ll modm(ll a,ll b)
{
	return (a*b)%MOD;
}
void pre()
{
	int i;
	fact[0]=1;
	F(i,1,200005){
		fact[i]=modm(fact[i-1],(ll)i);
	}
}
int fi(int n) 
{ 
	int result = n; 
	for(int i=2;i*i <= n;i++) 
	{ 
		if (n % i == 0) result -= result / i; 
		while (n % i == 0) n /= i; 
	} 
	if (n > 1) result -= result / n; 
	return result; 
} 
unsigned long long modPow(unsigned long long x,unsigned long long y)
{
	unsigned long long r=1, a=x;
	while (y > 0) {
		if ( (y&1)==1 ) {
			r = (r * a) % MOD;
		}
		a = (a * a) % MOD;
		y /= 2;
	}

	return r;
}
// Modular multiplicative inverse through Fermat's little theorem:
unsigned long long modInverse(long x,int y)
{
	return modPow(x, y-1);
}
int main()
{
	int n,i,cnt=0;
	ll ans1,ans2;
	S(n);
	F(i,0,n){
		S(a[i]);
		mp[a[i]]++;
	}
	F(i,0,n){
		S(b[i]);
		mp[b[i]]++;
	}

	S(MOD);
	pre();

	F(i,0,n){
		if(a[i]==b[i]){
			cnt++;
		}
	}

	ans1=modPow(2,cnt);
	ans2=1;
	map<int,int> ::iterator it;
	for(it=mp.begin();it!=mp.end();it++){
		//cout<<(*it).first<<" "<<(*it).second<<" "<<fact[(*it).second]<<endl;
		ans2=modm(ans2,fact[(*it).second]);
	}

	if(ans1==0)ans1=1;
        if(ans2==0)ans2=1;
	cout<<modm(ans2,modInverse(ans1,fi(MOD)))<<endl;
  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
11 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

I want to point out something, Fermat's little theorem can get the modular inverse for a number under PRIME mods only. This is the essential requirement the proof relies on. The modular inverse exists only when both numbers are coprime, in case the mod is NOT prime, you need to use the extended euclid algorithm to get the modular inverse.