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] ...
随机推荐
- ant有什么用
内容摘要: ANT是一个基于Java的自动化脚本引擎,脚本格式为XML.除了做Java编译相关任务外,ANT还可以通过插件实现很多应用的调用. 1)ANT的基本概念: 2)ANT的安装:解包,设置路径 ...
- EventBus3.0使用总结
在Android中,接口回调已经能够处理掉大部分业务需求了,实在太变态的需求就用广播也能够完成,自己写的性能好出问题也好解决.....工作需要,不得不看看EventBus的用法,今天就来介绍一下学习经 ...
- android的activity的跳转
1.无参数的跳转 先在layout下建立一个factivity.xml,在里面添加2个Button按钮和一个TextView,并添加属性 , 然后建立以个sactivity.xml文件, 在src下建 ...
- Android_listView_exc
listView布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- Adapter 适配器模式
将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 目标接口(Target):客户所期待的接口.目标可以是具体的或抽象的类,也可 ...
- sublime Text 3 Package Control 安装代码
ctrl+`,调出控制台,输入一下代码,回车,重启软件即可. import urllib.request,os; pf = 'Package Control.sublime-package'; ipp ...
- Git 常用配置和使用
Git:是一个分布式的源代码管理工具,Linux内核的代码就是用Git管理的所以它很强,也很快, 和 Vss/SVN比起来 本地Git初始化配置及其使用: 1. 初始化本地Git库:打开Git Bas ...
- JS实现各种页面的刷新
JS实现各种页面的刷新功能 1.刷新当前页面 opener.location.replace(opener.location.href); 或者window.opener.window.locatio ...
- works-er
- FOR XML PATH 应用及其反向分解
数据库环境:SQL SERVER 2005 我们实现将同一组的数据内容合并到一行的时候,可以通过FOR XML PATH来实现. 有数据如图1,要实现图2的效果 1.图1到图 ...