hdu2481

先算出总的状态转移方程:dp[i]  = 3*dp[i-1]-dp[i-2]+2; 与bzoj1002题转移方程相同。

因为n对于m可能无逆元,所以mod = n*m。可能会爆longlong。所以用类比于快速幂的写法,写快速乘。然后用矩阵快速幂+欧拉函数优化。

Toy

Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 710    Accepted Submission(s): 377


Problem Description
On birthday, Anthony got a toy. It is constructed with N+1(N>=3) 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
#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 7;
LL mod;
bool is_prime[maxn];
int prime[maxn], tail;
struct matrix {
    LL m[3][3];
};
LL A[3][3] = {{3, -1, 1}, {1, 0, 0}, {0, 0, 1}};
LL qmul(LL a, LL b) {
    LL ret = 0;
    a %= mod;
    b %= mod;
    if(a < 0) a += mod;
    if(b < 0) b += mod;

    while(b) {
        if(b & 1) ret = (ret + a) % mod;
        a = (a+a) % mod;
        b >>= 1;
    }
    return ret;
}

void mul(matrix &A, matrix &B) {
    matrix C;
    memset(C.m, 0, sizeof(C.m));
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            for(int k = 0; k < 3; k++)
                C.m[i][j] = (C.m[i][j] + qmul(A.m[i][k],B.m[k][j])) % mod;
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            A.m[i][j] = C.m[i][j];
}

matrix qpow(matrix a, LL n) {
    matrix ret;
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            ret.m[i][j] = (i == j);

    while(n) {
        if(n & 1) mul(ret, a);
        mul(a, a);
        n >>= 1;
    }
    return ret;
}

void getprime() {
    int n = 32000;
    for(int i = 2; i <= n; i++)
        is_prime[i] = 1;
    for(int i = 2; i <= n; i++) {
        if(is_prime[i]) {
            prime[++tail] = i;
            for(int j = 2 * i; j <= n; j += i) {
                is_prime[j] = 0;
            }
        }
    }

}

LL phi(int n) {
    int ret = n;
    for(int i = 1; prime[i]*prime[i] <= n; i++) {
        if(n % prime[i] == 0) {
            ret -= ret / prime[i];
            while(n % prime[i] == 0) n /= prime[i];
        }
    }
    if(n > 1) ret -= ret / n;
    return ret;
}

LL f(LL n) {
    if(n == 1) return 1;
    if(n == 2) return 5;
    matrix temp;
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            temp.m[i][j] = A[i][j];
    matrix ret = qpow(temp, n - 2);
    LL ans = (qmul(5, ret.m[0][0]) + ret.m[0][1] + 2*ret.m[0][2]) % mod;
    return ans;
}

int main() {
#ifdef ___LOCAL_WONZY___
    freopen ("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
    int n, m;
    LL ans;
    getprime();
    while(~scanf("%d%d", &n, &m)) {
        ans = 0;
        mod = (LL) m * n;
        int i;
        for(i = 1; i * i < n; i++) {
            if(n % i == 0) {
                ans = (ans + qmul(phi(i), f(n / i)) ) % mod;
                ans = (ans + qmul(phi(n / i), f(i)) ) % mod;
            }
        }
        if(i * i == n) ans = (ans + qmul(phi(i), f(i))) % mod;
        cout << (ans/n) % (mod / n) << endl;
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值