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 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
随机推荐
- Office Web Apps Server(1)
Office Web Apps Server runs on one or more servers and provides browser-based Office file viewi ...
- 小程序js脚本模块化调用
可以将一些公共的代码抽离成为一个单独的 js 文件,作为一个模块.模块只有通过 module.exports 或者 exports 才能对外暴露接口. 1. common.js // common.j ...
- vue路由文档笔记
引入router this.$router 和 router 使用起来完全一样.我们使用 this.$router 的原因是我们并不想在每个独立需要封装路由的组件中都导入路由 可以在任何组件内通过 t ...
- UVA 437 The Tower of Babylon(DAG上的动态规划)
题目大意是根据所给的有无限多个的n种立方体,求其所堆砌成的塔最大高度. 方法1,建图求解,可以把问题转化成求DAG上的最长路问题 #include <cstdio> #include &l ...
- Linux arm64内核启动
原创翻译,转载请注明出处. arm64的异常模型由一组异常级别(EL0-EL3)组成.EL0,EL1有安全模式和非安全模式的区别.EL2是虚拟机管理级别并且只有非安全模式.EL3是最高优先级并且只存在 ...
- 获取JavaScript对象的方法
写定义一个对象,如var a = new Array(),debugger,然后执行F12控制台的开发者模式下,进入断点,断点里面 会显示所有的方法的. var a = new Array(); de ...
- 【原创】Sagger使用
Swagger使用 1. Spring MVC配置文件中的配置 <mvc:annotation-driven/> <context:component-scan base-packa ...
- elmentUI组件怎么绑定原生事件
el-input为例: <el-input id="user-input" type="textarea" placeholder="请换行输入 ...
- java对象-String的用法
以某字符串结尾:“”.endwith("") 字符串比较:equals(anotherstr) 命名遵循英文语法
- SSH整合需要的jar包
[struts相关jar] commons-fileupload-1.2.2.jar commons-io-2.0.1.jar commons-lang3-3.1.jar freemarker-2.3 ...