HDU2481 Toy
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 661 Accepted Submission(s): 363
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.
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.
4 10000
4 10
13
3
突然想起MH四baka
数学问题 递推 矩阵加速 快速乘 置换群 burnside引理 欧拉函数
考点真全,真带感
前置技能 本题要用的递推式 Bzoj1002 [FJOI2007]轮状病毒
置换群 旋转同构计数 POJ2154 Color
可以发现这题要求的生成树和轮状病毒那题一样,可以用同一个递推式子。
由于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的更多相关文章
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- POJ 2398 Toy Storage(计算几何)
题意:给定一个如上的长方形箱子,中间有n条线段,将其分为n+1个区域,给定m个玩具的坐标,统计每个区域中的玩具个数. 题解:通过斜率判断一个点是否在两条线段之间. /** 通过斜率比较点是否在两线段之 ...
- 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 ...
- 【BZOJ-1010】玩具装箱toy DP + 斜率优化
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8432 Solved: 3338[Submit][St ...
- ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))
祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...
- [LintCode] Toy Factory 玩具工厂
Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...
- POJ 2398 - Toy Storage 点与直线位置关系
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5439 Accepted: 3234 Descr ...
- BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP
1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...
- 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage
题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
随机推荐
- MySQL数据库服务器逐渐变慢分析与解决
一.检查系统的状态 通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲,这也可能不是一个正常的状态,因为cpu可 ...
- 从一个线上服务器警告谈谈backlog
缘起 双十一如期而至,此时的我因为在处理客户的一个问题已经陷入了忙碌.突然,不断接到驻场实施发来的反馈,都是相同的反馈--"客户端操作缓慢". 我现在负责的服务器是一台接口服务器, ...
- Objective-C反射机制
oc反射机制有三个用途: 1.获得Class Class LoginViewController = NSClassFromString(@"LoginViewController" ...
- 一步一步构建手机WebApp开发——页面布局篇
继上一篇:一步一步构建手机WebApp开发——环境搭建篇过后,我相信很多朋友都想看看实战案例,这一次的教程是页面布局篇,先上图: 如上图所示,此篇教程便是教初学者如何快速布局这样的页面.废话少说,直接 ...
- 一个知乎日报pwa
前几天写了一篇文章关于如何实现一个简单版的pwa应用,端午撸了一个简易版知乎日报pwa. 关于如何写一个pwa,这里就不多介绍了,请移步这里.应用使用vue+vuex+axios,API这里,这里做了 ...
- MySQL高可用之PXC安装部署
Preface Today,I'm gonna implement a PXC,Let's see the procedure. Framework Hostname IP P ...
- Linux 文件与目录管理命令
处理目录的常用命令 常见的处理目录的命令: ls: 列出目录 cd:切换目录 pwd:显示目前的目录 mkdir:创建一个新的目录,语法:mkdir [-mp] 目录名称 -m :配置文件的权限 -p ...
- 03-Mysql数据库----安装与管理
本节掌握内容: mysql的安装.启动 mysql破解密码 统一字符编码 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的 ...
- popen和system问题
popen和system问题 1. 问题描述 C的代码里面去调用命令启动一个shell脚本,分别使用了下面两个途径. 其中一个是: func1(cmd) { popen(cmd,type); pclo ...
- deeplearning.ai课程学习(4)
第四周:深层神经网络(Deep Neural Networks) 1.深层神经网络(Deep L-layer neural network) 在打算使用深层神经网络之前,先去尝试逻辑回归,尝试一层然后 ...