http://www.spoj.pl/problems/AMR10I
数论递归+处理
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std;
#define foru(i, a, b) for(int i=(a); i<=(b); i++)
#define ford(i, a, b) for(int i=(a); i>=(b); i--)
#define clr(a) memset(a, 0, sizeof(a))
typedef long long ll;
ll p;
int n, m, num, tot;
int pri[20] = {1, 2, 3, 5, 7, 11, 13,
17, 19, 23, 29, 31, 37,
41, 43, 47, 53, 59, 61,
67};
map<ll,int> s;
void dfs(int d, int sum, ll c){
if (sum < 0) return;
s[c]=1;
ford(i, d, 1){
dfs(i, sum-pri[i], (c*pri[i])%p);
}
}
int main(){
// freopen("date.in", "r", stdin);
int T; scanf("%d", &T);
while (T--){
s.clear();
scanf("%d%lld", &n, &p);
dfs(19, n, 1ll);
printf("%d\n", s.size());
}
return 0;
}