下面的做法会超时
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Sereja asks you to find out number of arrays A of size N, i.e. A1, A2, ..., AN (1 ≤ Ai ≤ M), such that for each pair i, j (1 ≤ i < j ≤ N) gcd(Ai, Aj) = 1.
As the answer could be large, print it modulo 109 + 7 (1000000007).
Input
First line of the input contain an integer T - number of test cases.
T tests follow. First line of each test case contain two space separated integers N, M.
Output
For each test case output required answer modulo 10^9 + 7.
Constraints
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 105
- 1 ≤ M ≤ 100
Subtasks
- Subtask #1: (10 points, TL = 5 secs) 1 ≤ N, M ≤ 10
- Subtask #2: (25 points, TL = 5 secs) 1 ≤ N ≤ 100
- Subtask #3: (65 points, TL = 5 secs) original
Example
Input: 2 1 1 3 3 Output: 1 13
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
int gcd[110][110];
int ggcd(int a,int b){
if(b==0)return a;
return ggcd(b,a%b);
}
const int N=1e5+10;
int m,n;
int ans;
int val[N];
void dfs(int cur){
if(cur==n){
ans++;
return ;
}
for(int i=1;i<=m;i++){
if(cur==0){
val[cur]=i;
dfs(cur+1);
}else{
val[cur]=i;
bool flag=true;
for(int j=0;j<cur;j++)
if(gcd[val[cur]][val[j]]!=1){
flag=false;
break;
}
if(flag)dfs(cur+1);
}
}
}
int main(){
for(int i=1;i<=100;i++)
for(int j=i;j<=100;j++)
gcd[i][j]=gcd[j][i]=ggcd(i,j);
int t;
cin>>t;
while(t--){
cin>>n>>m;
ans=0;
dfs(0);
cout<<ans<<endl;
}
}