Question 2751: [haoi2012] Easy question (easy) Time Limit: 10 sec memory limit: 128 MB
Submit: 906 solved: 390
[Submit] [Status] Description
In order to make everyone happy, Xiao Q specially made a simple question (easy) to satisfy everyone. This simple question is described as follows:
There is a Series A that is known to be 1 ~ for all a [I ~ The Natural Number of N, and know which values cannot be obtained for some a [I]. We define the product of a series as the product of all elements of the series, is it easy to obtain the product of all possible series and the value of MOD 1000000007? Haha!
Input
The three integers n, m, and K in the first row represent the value range, number of elements, and the number of known restrictions.
In the next K rows, each row has two positive integers x, and y indicates that the value of a [x] cannot be y.
Output
An integer in a row indicates the product of all possible series and the result after modulo 1000000007. If no valid sequence exists, the answer is 0.
Sample input3 4 5
1 1
1 1
2 2
2 3
4 3
Sample output90
Example
A [1] cannot take 1
A [2] cannot go to 2 or 3
A [4] cannot take 3
Therefore, there are 12 possible series:
Series Product
2 1 1 1 2
2 1 1 2 4
2 1 2 1 4
2 1 2 2 8
2 1 3 1 6
2 1 3 2 12
3 1 1 3
3 1 1 2 6
3 1 2 1 6
3 1 2 2 12
3 1 3 1 9
3 1 3 2 18
Hint
Data range
30% of Data n <= 4, m <= 10, k <= 10
Another 20% of Data K = 0
70% of Data n <= 1000, m <= 1000, k <= 1000
100% of Data n <= 109, m <= 109, k <=, 1 <= Y <= N, 1 <= x <= m
Question
It is easy to find that the answer is the product of the possible values of each digit, and then the K of the question is very small, so the number of modifications is very small, so most of them are the same, so we can use the quick power! Okay, I just want to say that STL is good! Although it will slow down some Qaq
Code
1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<set> 4 #include<map> 5 #include<iostream> 6 using namespace std; 7 8 const int M=1000000007; 9 10 map<int,int> minu;11 map<int,int>::iterator it;12 set<string> hash;13 14 long long Sum=0;15 int n,m,k;16 long long Ans=1;17 18 inline long long getTimes(long long n,int k){19 long long tmp=n;n=1;20 while(k){21 if (k&1) n=n*tmp%M;22 k/=2;23 tmp=tmp*tmp%M;24 } 25 return n;26 }27 28 inline string i2s(int x){29 string str;30 while(x){31 str=str+(char)(x%10+‘0‘);32 x/=10;33 }34 return str;35 }36 37 int main(){38 scanf("%d%d%d",&n,&m,&k);39 Sum=((long long)n*((long long)n+(long long)1)/(long long)2)%M;40 for (int i=1;i<=k;i++){41 int x,y;42 scanf("%d%d",&x,&y);43 if (hash.insert(i2s(x)+"-"+i2s(y)).second==false) continue;44 it=minu.find(x);45 if (it==minu.end()){46 minu[x]=y%M;47 }else{48 minu[x]=(minu[x]+y)%M;49 }50 }51 for (it=minu.begin();it!=minu.end();it++){52 if ((*it).second%M<=Sum)53 Ans=Ans*(Sum-(*it).second%M)%M;54 else55 Ans=Ans*(Sum-(*it).second%M+M)%M;56 }57 Ans=Ans*getTimes(Sum,m-minu.size())%M;58 59 printf("%lld\n",Ans);60 return 0;61 }
View code
Bzoj 2751: [haoi2012] Easy question (easy)