int main()
{  long int t,n,m,q,a,b,r,i,temp;
scanf("%ld",&t);
while(t--)
{
    scanf("%ld%ld%ld",&n,&m,&q);
    while(q--)
    { a=1;
        scanf("%ld",&r);
        b=n-r;
        if(n<=5)
        {


        i=2;
        while(i<=n)
        {a=a*pow(i,i);i++;
        }
       // printf("%llu\n",a);
        i=2;
        while(i<=r)
        {a=a/(int)pow(i,i);i++;
        }
             //   printf("%llu\n",a);

        i=2;
        while(i<=b)
        {a=a/(int)pow(i,i);i++;
        }
       // printf("%llu\n",a);

    printf("%ld\n",a%m);
        }
        else
        {
            if(n-r>r)
            {
               temp=n-r;
               b=r;
               r=temp;
            }
            i=r+1;
             while(i<=n)
        {a=(a*fastpower(i,i,m))%m;i++;
        }
        i=2;
         while(i<=n-r)
        {a=(a*fastpower(fastpower(i,i,m),m-2,m))%m;i++;
        }
          printf("%ld\n",a);
        }
}
}
    return 0;
}
Exemple #2
0
int arctan(struct NUMBER *a,struct NUMBER *b)
{
	struct NUMBER i,tmp1,tmp2;
	struct NUMBER apow,tenpow,mone,n;//計算に使う変数
	int j,res;

	clearByZero(b);
	clearByZero(&i);
	clearByZero(&tmp1);
	clearByZero(&tmp2);
	clearByZero(&apow);
	clearByZero(&tenpow);
	setInt(&n,1);

	tenpow.n[KETA-1] = 1;//tenpowは10^(KETA-1)
	j = 0;
	while(1)
	{
		res = fastpower(a,&n,&apow);//apow = a^n
		if(res != 0)
			break;

		res = multiple(&n,&apow,&tmp1);//tmp1 = n * apow
		if(res != 0)
			break;
		res = divide(&tenpow,&tmp1,&tmp2,&apow);//tmp2 = tenpow/tmp1,apowは使わないからこれ以降使わないから


		if(res != 0)
			break;
		if(i.n[0]%2)//iが奇数なら,手っ取り早く偶奇を見たいからこう
			setSign(&tmp2,-1);
		else //iが偶数なら
			setSign(&tmp2,1);
		copyNumber(b,&tmp1);

		add(&tmp1,&tmp2,b);//b += (1/n * 1/(a^n)) * 10^(KETA-1),オーバーフローなどはないので戻り値は保存しない
		if(firstNotZero(&tmp2) <= KETA/10)//(1/i * 1/(a^i)) * 10^(KETA-1)がKETAの1/10程度になったら
			break;

		/*2i +1の操作*/
		copyNumber(&i,&tmp1);//i → tmp 
		increment(&tmp1,&i);//i++
		copyNumber(&i,&tmp1);//i → tmp1
		add(&tmp1,&i,&n);//n = 2i(n= i + i)
		copyNumber(&n,&tmp1);//i → tmp 
		increment(&tmp1,&n);//i++
	}
	return(res);
}
int main()
{long int t,n,q,b,r,i,temp,x;
long long int a,m;
t=inp();
//scanf("%ld",&t);
//seive();
calc();
while(t--)
{//scanf("%ld%llu%ld",&n,&m,&q);
    n=inp();
    scanf("%llu",&m);
    q=inp();
  while(q--)
  {for(i=1;i<=95;i++)
        total[i]=0;
        r=inp();
          //scanf("%ld",&r);
        b=n-r;
        a=1;
         if((n-r)>r)
            {
               temp=n-r;
               b=r;
               r=temp;
            }
            for(x=1;x<=95;x++)
            {
                total[x]=fact[n][x]-fact[r][x];
            }
            for(x=1;x<=95;x++)
                 total[x]=total[x]-fact[n-r][x];
            for(i=1;i<=95;i++)
         {
             if(total[i])
             {
                 a=(a*fastpower(pr[i],total[i],m))%m;
             }
         }
          printf("%llu\n",a);
  }
 
}
    return 0;
}
void decrypt(char *str) {
    int i, j, len;     // len: the number of blocks (of 6) in string after step 1
    char *oristr;   // original string
    char *tmpstr;   // temporary string
    unsigned long long code[24];  // used in step 2, decimal version of encrypted 41base code
    const unsigned long long base[6] = {1, 41, 1681, 68921, 2825761, 115856201};  // 41^0, 41^1, ... 41^5
    const int dict1[128] = {
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,31, 0,27,37, 0,39,30,
    33,34, 0, 0,29,35,28,38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,36, 0,
     0, 0, 0,32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,40, 0, 0, 0, 0, 1, 2, 3,
     4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,
    24,25,26, 0, 0, 0, 0, 0
    };  // dictionary: char(ascii) --> int coefficient
    const char dict2[41] = {
    ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
    'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y', 'z', '#', '.', ',',
    '\'', '!', '?', '(', ')', '-', ':', '$', '/', '&',
    '\\'
    };  // dictionary: int code (from base 41) --> char

    // initialization
    oristr = (char*)malloc(sizeof(char) * 200);
    tmpstr = (char*)malloc(sizeof(char) * 8);
    strcpy(oristr, str);
    memset(str, 0, 200);

    // step 1:
    i = 0;
    while (i < strlen(oristr)) {
        if ((i % 8) == 0) { // reset 7-char tmp string
            memset(tmpstr, 0, 8);
        }
        if ((i % 8) == 7) { // ignore every 8th char
            strcat(str, tmpstr);    // concatenate the temp string to str
            i++;
            continue;
        }
        tmpstr[strlen(tmpstr)] = oristr[i];
        i++;
    }
    if (i % 8 > 0)
        strcat(str, tmpstr);    // concatenate the rest of the string

    // step 2:
    memset(code, 0, 24*sizeof(unsigned long long));
    for (i = 0; i < strlen(str); i++) {
        code[i / 6] = (code[i / 6] + dict1[str[i]] * base[5 - (i % 6)]) % 4294434817;
    }
    len = strlen(str) / 6;

    // step 3:
    for (i = 0; i < len; i++) {
        if (code[i] != 0)
            code[i] = fastpower(code[i], 1921821779, 4294434817);
    }

    // step 4:
    memset(str, 0, 200);
    for (i = 0; i < len; i++) {
        for (j = 5; j >= 0; j--) {
            str[strlen(str)] = dict2[code[i] / base[j]];
            code[i] %= base[j];
        }
    }

    free(oristr);
    free(tmpstr);
}