bzoj 3328: PYXFIB 数论
3328: PYXFIB
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 130 Solved: 41
[Submit][Status][Discuss]
Description

Input
第一行一个正整数,表示数据组数据 ,接下来T行
每行三个正整数N,K,P
Output
T行,每行输出一个整数,表示结果
Sample Input
1 2 3
Sample Output
HINT

Source
思路与莫比乌斯反演相似,通过二项式巧妙地解决组合数的问题,总结一个技巧:对于同余系P,g为原根,w=g^((p-1)/k),那么sigma(w^(ij))==[i mod k==0],其他一些非人类的插值,代换这里就不多说了。太科幻了。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long qword;
int mod;
qword pow_mod(qword x,qword y,qword mod=::mod)
{
qword ret=;
while (y)
{
if (y&)
ret=ret*x%mod;
x=x*x%mod;
y>>=;
}
return ret;
}
struct matrix
{
qword mat[][];
matrix()
{
memset(mat,,sizeof(mat));
}
void Init0()
{
memset(mat,,sizeof(mat));
mat[][]=mat[][]=;
}
void Init1()
{
memset(mat,,sizeof(mat));
mat[][]=;
mat[][]=mat[][]=;
}
void Print()
{
for (int i=;i<;i++)
{
for (int j=;j<;j++)
{
printf("%lld ",mat[i][j]);
}
printf("\n");
}
printf("\n");
}
};
matrix operator *(matrix m1,matrix m2)
{
matrix ret;
for (int k=;k<;k++)
{
for (int i=;i<;i++)
{
for (int j=;j<;j++)
{
ret.mat[i][j]=(ret.mat[i][j]+m1.mat[i][k]*m2.mat[k][j]%mod)%mod;
}
}
}
return ret;
}
matrix operator *(matrix m1,qword k)
{
for (int i=;i<;i++)
for (int j=;j<;j++)
m1.mat[i][j]=m1.mat[i][j]*k%mod;
return m1;
}
matrix operator +(matrix m1,matrix m2)
{
for (int i=;i<;i++)
for (int j=;j<;j++)
m1.mat[i][j]=(m1.mat[i][j]+m2.mat[i][j])%mod;
return m1;
} matrix matrix_pow(matrix m1,qword y)
{
matrix res;
res.Init0();
while (y)
{
if (y&)
res=res*m1;
m1=m1*m1;
y>>=;
}
return res;
}
void Analyse(int x,vector<int> &vec)
{
for (int i=;i*i<=x;i++)
{
if (i*i==x){
vec.push_back(i);
sort(vec.begin(),vec.end());
return ;
}
if (x%i==)
{
vec.push_back(i);
vec.push_back(x/i);
}
}
sort(vec.begin(),vec.end());
} int main()
{
// freopen("input.txt","r",stdin);
int nn;
scanf("%d",&nn);
while (nn--)
{
qword n;
int t,p;
scanf("%lld%d%d",&n,&t,&p);
mod=p;
int g=-;//原根
vector<int> fpm1;
Analyse(p-,fpm1);
// for (int i=0;i<fpm1.size();i++)
// printf("%d ",fpm1[i]);
// printf("\n");
for (int i=;i<p;i++)
{
bool flag=true;
for (int j=;j<(int)fpm1.size()-;j++)
{
if (pow_mod(i,fpm1[j])==)
{
flag=false;
break;
}
}
if (flag)
{
g=i;
break;
}
}
/*
qword x0=1;
for (int i=1;i<p-2;i++)
{
x0=x0*g;
if (x0==1)throw 1;
}*/
qword w=pow_mod(g,(p-)/t);
qword invw=pow_mod(w,mod-);
qword xnow=;
qword ixnow=;
matrix res;
for (int i=;i<t;i++)
{
matrix matx;
matx.Init1();
matx.mat[][]=(matx.mat[][]+xnow)%mod;
matx.mat[][]=(matx.mat[][]+xnow)%mod;
matx=matrix_pow(matx,n);
matx=matx*pow_mod(ixnow,n);
res=res+matx;
xnow=xnow*invw%mod;
ixnow=ixnow*w%mod;
}
res=res*pow_mod(t,mod-);
printf("%lld\n",res.mat[][]);
}
}
bzoj 3328: PYXFIB 数论的更多相关文章
- BZOJ 3328: PYXFIB 解题报告
BZOJ 3328: PYXFIB 题意 给定\(n,p,k(1\le n\le 10^{18},1\le k\le 20000,1\le p\le 10^9,p \ is \ prime,k|(p- ...
- bzoj 3328 PYXFIB——单位根反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3328 单位根反演主要就是有 \( [k|n] = \frac{1}{k}\sum\limit ...
- bzoj 3328 PYXFIB —— 单位根反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3328 单位根反演,主要用到了 \( [k|n] = \frac{1}{k} \sum\lim ...
- bzoj 3328 : PYXFIB
Discription Input 第一行一个正整数,表示数据组数据 ,接下来T行每行三个正整数N,K,P Output T行,每行输出一个整数,表示结果 Sample Input 1 1 2 3 S ...
- BZOJ 3328: PYXFIB 单位根反演+矩阵乘法+二项式定理
如果写过 LJJ 学二项式那道题的话这道题就不难了. #include <bits/stdc++.h> #define ll long long #define setIO(s) freo ...
- 【BZOJ3328】PYXFIB 数论+矩阵乘法
[BZOJ3328]PYXFIB Description Input 第一行一个正整数,表示数据组数据 ,接下来T行每行三个正整数N,K,P Output T行,每行输出一个整数,表示结果 Sampl ...
- BZOJ 2142 礼物 数论
这道题是求组合数终极版. C(n,m) mod P n>=1e9 m>=1e9 P>=1e9且为合数且piqi<=1e5 拓展lucas定理. 实际上就是一点数论小知识的应用. ...
- bzoj 2818 GCD 数论 欧拉函数
bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...
- Bzoj 2456: mode 数论,众数
2456: mode Time Limit: 1 Sec Memory Limit: 1 MBSubmit: 2843 Solved: 1202[Submit][Status][Discuss] ...
随机推荐
- chrome浏览器频频崩溃,如何解决?
之前chrome常崩溃,也没有找到原因,就将就着用吧,一直用到今天, 今天连续几次崩了,突然想到,难道是因为我访问的域名没有解析(能想到这个,是因为今天分配公司域名测试的时候常输错),于是就输入一个不 ...
- AWS 命令行界面 + Python 的 AWS 开发工具包 (Boto3)
安装AWS CLI $ pip install awscli 安装Boto3 $ pip install boto3 设置AWS CLI $ aws configure AWS Access Key ...
- spring mvc 3.1的自动注入参数遇到的问题
在网上下载了xheditor作为页面的编辑器,编辑内容后post到后台保存,后台方法用spring mvc的自动注入的方式接收参数. 这种方式在各个浏览器下运行良好,但是在ie11下发现,从word. ...
- 简单的实现QQ通信功能(二)
第二部分:功能需求以及大体思路 一:功能需求: 1. 角色:登录用户. 2. 登录: (1)检查用户名和密码是否正确,正确登录成功,否则提醒用户名或密码错误. (2)登录时可以选择登录状态,送入数据库 ...
- linux-ssh远程后台执行脚本-放置后台执行问题(转)
写了一个监控负载的小脚本(死循环,测试结束后再kill对应进程),因需要监控多台服务器,所以在一台服务器上使用ssh统一执行脚本遇到问题:使用ssh root@172.16.146.20 '/usr/ ...
- .net+easyui--combobox
一:预定义结构的 select 元素创建组合框(combobox)值固定写死 <select class="easyui-combobox" name="state ...
- Android 设计随便说说之简单实践(消息流动)
在上面两篇分别说明了设计中较为简单也是很关键的实践点. 第一模块划分,它是根据每个模块所承载的业务,进行划分,是应用程序一个静态的描述. 第二合理组合,它是是将每个模块调动起来,共同实现业务,是一个准 ...
- oracle 11g不能连接报ORA-12537+ora-609解决
操作系统: windows2008 数据库:oracle11g 问题发生:一套正常跑了一年的应用系统,忽然无法连接上数据库,但是另外一个应用可以正常链接,数据入库正常. 数据库服务器端查看: 使用 l ...
- Xcode8 Could not build Objective-C module 'FBSDKCoreKit'
解决方法是: 删除/Users/Rinpe/Library/Developer/Xcode/DerivedData下对应的文件夹即可.
- mysql error笔记1
mysql视图问题: The user specified as a definer ('root'@'%') does not exist 原因:由于root用户对全局host无访问权限,给root ...