hdu2481
来源:互联网 发布:淘宝动态计算器 编辑:程序博客网 时间:2024/06/06 23:18
先算出总的状态转移方程: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.
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.
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 100004 100004 10
Sample Output
6133
#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
- hdu2481
- hdu2481 Toy
- HDU2481:Toy(Burnside)
- HDU2481 Toy (数论好题)
- 【hdu2481】Toy,burnside引理+矩阵乘法
- [HDU2481]Toy(置换+矩阵乘法)
- 23个设计模式--(一)、单例模式
- 机器学习之分类学习(模型训练)
- lintcode 二叉树的层次遍历
- Flask零基础到项目实战(六)模型分文件
- 给div加focus和查看当前focus的元素
- hdu2481
- 处理机管理功能
- 一些富有创意的Demo
- shiro-spring 结合的配置问题
- struts2学习心得
- 粗体判断js中的数据类型有一下几种方法
- 国内yum安装MariaDB速度慢的问题
- 1063:计算两点间的距离
- 一个三级联动的地址选择器