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] ...
随机推荐
- C# Obsolete
Obsolete 属性将某个程序实体标记为一个建议不再使用的实体.每次使用被标记为已过时的实体时,随后将生成警告或错误,这取决于属性是如何配置的 如果把false 改成 true 的话那么GetNam ...
- web项目设计与开发——DBHelper2
第二次学习的内容是根据DBHelper遍历出数据库中的所有数据. 具体内容为: 一.编写程序 1.创建工程——userMangager 2.在src目录下创建四个包,分别为DAO,DB ...
- .net+easyui系列--按钮
easyui提供了各种按钮样式,包括搜索.新增.保存.删除等 <a id="btn" href="#" class="easyui-linkbu ...
- Spring中整合Titles
在<Spriing实战(第三版)>这本书中,有一个使用titles的例子,但是这是一个不完整的例子.那么要参照起来就比较难了,于是找到了下面这篇博客. 在Spring中使用tiles2 ( ...
- OC加强-day04
#pragma mark 00知识回顾 //定义一个函数 函数没有返回值函数有一个参数:返回值是double 参数是两个int的block void test(int a); void test(do ...
- O-C相关05:方法的封装.
前言:在 OC 中进行封装, 就是实现设置实例变量和获取实例变量数据的方法, 常常称为 setter 方法和 getter 方法. 或称为 get set 读写器. 1,setter 方法 sette ...
- 如何在Angular2中使用jquery
首先在index.html中引入jquery文件 <script src="http://cdn.bootcss.com/jquery/2.1.3/jquery.js"> ...
- 《JavaScript高级程序设计》 阅读计划
第一周 第1章 JavaScript简介 1 第2章 在Html中使用JavaScript 1 第3章 基本概念 3 第二周 第4章 变量.作用域和内存 ...
- HttpUtility.HtmlEncode
HttpUtility.HtmlEncode用来防止站点受到恶意脚本注入的攻击 public string Welcome(string name, int numTimes = 1) { r ...
- linux ptheard 生产者消费者
; { { printf( pthread_mutex_lock(&mutex); != g_iBufSiz ...