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

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
 
Source
 
Recommend
lcy

突然想起MH四baka

数学问题 递推 矩阵加速 快速乘 置换群 burnside引理 欧拉函数

考点真全,真带感

前置技能 本题要用的递推式 Bzoj1002 [FJOI2007]轮状病毒

     置换群 旋转同构计数 POJ2154 Color

     快速乘 HDU5187 zhx's contest

可以发现这题要求的生成树和轮状病毒那题一样,可以用同一个递推式子。

由于n很大,不能直接递推,需要矩阵乘法优化。

然后在外面套一个burnside引理即可。

由于M不一定是质数,不能求逆元,为了保证除法正确性,需要在mod (n*M)的意义下计算,才可以/n

(n*M)的范围是1e18,这使得普通乘法会爆LL,需要加一个快速乘优化。

快速乘不支持乘负数的样子,所以把递推矩阵里的-1加到mod-1,在模意义下等价

理清思路以后就是按模块把代码堆上去,写起来挺爽的。

namespace没什么卵用,但是莫名帅啊

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL mod;
namespace Euler{
int pri[mxn],cnt=;
bool vis[mxn];
void init(){
for(int i=;i<mxn;i++){
if(!vis[i])
pri[++cnt]=i;
for(int j=;j<=cnt && (LL)pri[j]*i<mxn;j++){
vis[pri[j]*i]=;
if(i%pri[j]==)break;
}
}
return;
}
LL phi(LL x){
LL res=x;
for(int i=;i<=cnt && pri[i]<=x;i++){
if(x%pri[i]==){
res=res/pri[i]*(pri[i]-);
while(x%pri[i]==)x/=pri[i];
}
}
if(x>)res=res/x*(x-);
return res%mod;
}
}
int n,M;
LL f1,f2;
LL ksmul(LL a,LL k){
LL res=;
// printf("ksmul:%lld %lld\n",a,k);
while(k){
if(k&)res+=a; if(res>=mod)res-=mod;
a<<=; if(a>=mod)a-=mod;
k>>=;
}
// printf("d\n");
return res;
}
struct Mat{
LL x[][];
Mat operator * (Mat b){
Mat res;
for(int i=;i<=;i++)
for(int j=;j<=;j++){
res.x[i][j]=;
for(int k=;k<=;k++){
(res.x[i][j]+=ksmul(x[i][k],b.x[k][j]))%=mod;
// printf("i:%d j:%d k:%d\n",i,j,k);
}
}
return res;
}
void init(){
memset(x,,sizeof x);
x[][]=;
x[][]=;
x[][]=;
return;
}
}mp,now;
void ksm(Mat a,LL k){
now.init();
while(k){
if(k&)now=now*a;
a=a*a;
k>>=;
}
return;
}
LL solve(int k){
if(k==)return ;
if(k==)return ;
// printf("solving %d\n",k);
ksm(mp,k-);
// printf("solved %d %lld %lld %lld\n",k,now.x[1][1],now.x[1][2],now.x[1][3]);
return now.x[][];
}
int main(){
using namespace Euler;
int i,j;
init();
mp.x[][]=;
// mp.x[2][1]=-1;
mp.x[][]=;mp.x[][]=;
mp.x[][]=;
while(scanf("%d%d",&n,&M)!=EOF){
mod=(LL)n*M;
mp.x[][]=mod-;
LL ans=;
for(i=;i*i<n;i++){
if(n%i==){
(ans+=ksmul(solve(i),phi(n/i)))%=mod;
(ans+=ksmul(solve(n/i),phi(i)))%=mod;
}
}
if(i*i==n) (ans+=solve(i)*phi(i))%=mod;
ans/=n;
printf("%lld\n",ans);
}
return ;
}

HDU2481 Toy的更多相关文章

  1. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  2. POJ 2398 Toy Storage(计算几何)

    题意:给定一个如上的长方形箱子,中间有n条线段,将其分为n+1个区域,给定m个玩具的坐标,统计每个区域中的玩具个数. 题解:通过斜率判断一个点是否在两条线段之间. /** 通过斜率比较点是否在两线段之 ...

  3. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

  4. 【BZOJ-1010】玩具装箱toy DP + 斜率优化

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8432  Solved: 3338[Submit][St ...

  5. ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))

    祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...

  6. [LintCode] Toy Factory 玩具工厂

    Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...

  7. POJ 2398 - Toy Storage 点与直线位置关系

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5439   Accepted: 3234 Descr ...

  8. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP

    1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...

  9. 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage

    题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...

随机推荐

  1. node 发送 post 请求 get请求。

    因为我们部门打算用node请求restful 然后慢慢替换掉服务端,以后直接请求soa的接口,让前端的数据更贴切项目,因为我们服务端接口和app公用一套,由于业务的需求和版本不统一(例如app6.4的 ...

  2. 剖析DI

    0x00.前言 当我们研究一些晦涩的源码,上网查阅资料的时候,映入眼帘的总有这么些名词:DIP.IOC.DI.DL.IOC容器这些专业名词.如果不懂这些名词背后的含义,我们内心有可能是这样的: 0x0 ...

  3. Windows下nginx作为静态资源服务器使用

    一.Nginx下载与安装 1.nginx官方下载地址:http://nginx.org/ 2.下载完后将压缩包解压即可 3.nginx配置文件为根目录下conf\nginx.conf 二.Nginx常 ...

  4. 给虚拟机发送键盘按键key

    使用举例:virsh send-key 11 KEY_LEFTCTRL KEY_LEFTALT KEY_DELETE作用:发送"ctrl+alt+del"给虚拟机,linux虚拟机 ...

  5. Linux服务架设篇--ping命令

    工作原理: 向远程机发送包含一定字节数的ICMP数据包,如果能收到对方的回复的数据包,就表明网络是相通的,而且根据两个数据包的时间差,还可以知道相互之间网络链接的速度. 注意: 有些远程主机由于某种原 ...

  6. 容器基础(三): 使用Cgroups进行资源限制

    Linux Cgroups Linux Cgroups 是 Linux 内核中用来为进程设置资源限制的一个重要功能. Cgroups将进程进行分组, 然后对这一组进程进行统一的资源监控和限制.Cgro ...

  7. JAVA课程设计 俄罗斯方块

    俄罗斯方块 可实现功能 1.账号管理:登录.注册 2.游戏实现:移动.旋转.消除方块统计得分.暂停游戏.暂停后继续游戏.此轮游戏未结束开启新一轮游戏.游戏未结束退出游戏. 3.排行榜:按分数排名.按局 ...

  8. A - 移动的骑士

    A - 移动的骑士 Time Limit: 1000/1000MS (C++/Others) Memory Limit: 65536/65536KB (C++/Others) Problem Desc ...

  9. Hangman游戏源代码 --- python实现

    #hangman.py from PythonCard import model,dialog import random def find_letters(letter,a_string): loc ...

  10. idea 控制台中文乱码

    idea 控制台中文乱码,网上找了好多基本都是说在tomcat配置文件里面添加-Dfile.encoding=UTF-8 添加后依然乱码, 需要在idea64.exe.vmoptions文件中添加-D ...