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. Jexus支持HTTPS协议

    众所周知,在HTTPS页面请求HTTP资料的时候,现代浏览器会拦截,提示用户是否继续,或者直接拦截,提示都不出来. 最近给自己做了个快速书签工具,点击书签就直接把书签发送到服务器地址,然后保存到我的网 ...

  2. 你了解的UIKit结构?

  3. 从循环里面用QPixmap new对象很耗时联想到的

    1.在循环里面用QPixmap new图片对象延迟很高,这个是通过打时间日志得出的,深层原因还不清楚: 2.自制的图片浏览器在初始化的时候会初始化自己的一个图片列表,所以要用到上面的描述.所有图片的初 ...

  4. Qt QLabel 播放GIF动画

    很久以前用过,不过慢慢的不用了,就慢慢的忘记了,今天拾起来,记录一下,以后用的时候可以翻一下 QLabel播放GIF动画其实很简单 第一步,需要包含头文件,Qt播放GIF动画,我使用的是QMovie类 ...

  5. Unity和Lua交互

    用lua就表示项目用到了热更新,通常每次热更新都会从服务器获取最新的lua脚本放到Android/ios设备的本地目录下,但是lua应该放到哪个目录下呢,这里就先说说lua里面的路径问题 1.不可以放 ...

  6. selenium 的安装使用

    直接pip安装 pip install selenium 默认是火狐浏览器,需要安装下面网址的软件,解压后加入到环境变量中就可以了 https://github.com/mozilla/geckodr ...

  7. Win7下搭建Zigbee开发环境

    操作系统:64位Win7 芯片类型:Texas Instruments的CC2530 软件平台:IAR v8.10 Zigbee协议栈:ZStack-CC2530-2.5.1a CP2102 USB ...

  8. EXEL文件转成简书MD表格

    EXEL文件转成简书MD表格 0.1.3 mac: https://github.com/fanfeilong/exceltk/blob/master/pub/exceltk.0.1.3.pkg wi ...

  9. 段寻址*****************************TBD

    fffff880`01b05be1 ff9708020000    call    qword ptr [rdi+208h] ds:002b:fffff980`0554ae88=fffffa8004b ...

  10. How to Create a Perl Based Custom Monitor on NetScaler

    How to Create a Perl Based Custom Monitor on NetScaler https://support.citrix.com/article/CTX227727 ...