Sicily 1916. Toy

来源:互联网 发布:淘宝宣传软文 编辑:程序博客网 时间:2024/06/06 01:03

1916. Toy

Constraints

Time Limit: 2 secs, Memory Limit: 32 MB

Description

 On birthday, Anthony got a toy. It is constructed with N + 1 (N3) balls and 2 * N sticks. All balls are in a same plane. One of them is special, while the other N balls are connected to it by N sticks with the same length. The angles between any two adjacent sticks are equal. And finally, any two adjacent balls(except the central one) are connected by a stick. Here are two examples:

Anthony wanted to remove N sticks, leaving all balls still connected. He wanted to know the number of all legal solutions. Your task is to solve this problem for him.

Notice that if a solution will be the same as another one by rotation, these two solutions should be consider as the same.

The answer may be quite large. You just need to calculate the remainder of the answer when divided by M .

Input

 Input contains several test cases.

For each test case, there is only one line containing two integers N and M (3 ≤ N ≤ 10^9, 2 ≤ M ≤ 10^9) .

Input is terminated by EOF.

Output

 For each case, output one integer in one line, representing the remainder of the number of all solutions when divided by M .

Sample Input

3 10000 4 10000 4 10

Sample Output

6 13 

3

// Problem#: 1916// Submission#: 3591556// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <string.h>long long realmod;long long mod;typedef long long matrix[2][2];const int maxb = 31;matrix bin[1 + maxb];int prime[100];int times[100];int np;long long mul(long long a, long long b) {    if (b == 0) return 0;    if (b == 1) return a;    long long c = mul(a, b / 2);    if (b % 2 == 1) return (c + c + a) % mod;    else return (c + c) % mod;}void multiply(matrix a, matrix b, matrix c) {    matrix tmp = {0};    for (int i = 0; i < 2; i++)        for (int j = 0; j < 2; j++) {            for (int k = 0; k < 2; k++) {                tmp[i][j] += mul(a[i][k], b[k][j]);                tmp[i][j] %= mod;            }        }    memcpy(c, tmp, sizeof(matrix));}void prework() {    matrix A = {        {3 % mod, 1},        {mod - 1, 0}    };    memcpy(bin[1], A, sizeof(matrix));    for (int i = 2; i <= maxb; i++) multiply(bin[i - 1], bin[i - 1], bin[i]);}long long F(int n) {    if (n == 1) return 1;    if (n == 2) return 5;    int k = n - 3;    matrix a = {        {8, 3},        {0, 0}    };    for (int i = 1; k > 0; k /= 2, i++) {        if (k & 1) multiply(a, bin[i], a);    }    long long fn = a[0][0] * 3 - a[0][1] * 2 - 2;    return ((fn % mod) + mod) % mod;}long long dfs(int t, int divisor, int euler) {    long long ret = 0;    if (t == np) {        return mul(euler % mod, F(divisor));    }    for (int i = 0; i < times[t]; i++) divisor *= prime[t];    for (int i = 0; i <= times[t]; i++) {        ret += dfs(t + 1, divisor, euler);        ret %= mod;        divisor /= prime[t];        if (i == 0) euler *= (prime[t] - 1);        else euler *= prime[t];    }    return ret;}int work(int n) {    mod = realmod * n;    prework();    np = 0;    int t = n;    for (int i = 2; i * i <= t; i++)        if (t % i == 0) {            prime[np] = i;            times[np] = 0;            while (t % i == 0) {                times[np]++;                t /= i;            }            np++;        }    if (t > 1) {        prime[np] = t;        times[np] = 1;        np++;    }    long long sum = dfs(0, 1, 1);    return (int)(sum / n);}int main() {    int n;    while (scanf("%d%lld", &n, &realmod) == 2) printf("%d\n", work(n));    return 0;}                                 


0 0
原创粉丝点击