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
1 2 3

Sample Output

1

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 数论的更多相关文章

  1. 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- ...

  2. bzoj 3328 PYXFIB——单位根反演

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3328 单位根反演主要就是有 \( [k|n] = \frac{1}{k}\sum\limit ...

  3. bzoj 3328 PYXFIB —— 单位根反演

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3328 单位根反演,主要用到了 \( [k|n] = \frac{1}{k} \sum\lim ...

  4. bzoj 3328 : PYXFIB

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

  5. BZOJ 3328: PYXFIB 单位根反演+矩阵乘法+二项式定理

    如果写过 LJJ 学二项式那道题的话这道题就不难了. #include <bits/stdc++.h> #define ll long long #define setIO(s) freo ...

  6. 【BZOJ3328】PYXFIB 数论+矩阵乘法

    [BZOJ3328]PYXFIB Description Input 第一行一个正整数,表示数据组数据 ,接下来T行每行三个正整数N,K,P Output T行,每行输出一个整数,表示结果 Sampl ...

  7. BZOJ 2142 礼物 数论

    这道题是求组合数终极版. C(n,m) mod P n>=1e9 m>=1e9 P>=1e9且为合数且piqi<=1e5 拓展lucas定理. 实际上就是一点数论小知识的应用. ...

  8. bzoj 2818 GCD 数论 欧拉函数

    bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...

  9. Bzoj 2456: mode 数论,众数

    2456: mode Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 2843  Solved: 1202[Submit][Status][Discuss] ...

随机推荐

  1. 实现一个脚本语言Raven(一)

    之前实现了Raven语言的0.1版,仅仅支持表达式处理与控制语句,由于不支持数组.函数.类,甚至都不是图灵完全的语言. 现在参考vczh的博客打算重新写一遍Raven语言.陈祖不愧是神啊,高中就写出支 ...

  2. Adapter 适配器模式

    将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 目标接口(Target):客户所期待的接口.目标可以是具体的或抽象的类,也可 ...

  3. 爬虫神器XPath,程序员带你免费获取周星驰等明星热门电影

    本教程由"做全栈攻城狮"原创首发,本人大学生一枚平时还需要上课,但尽量每日更新文章教程.一方面把我所习得的知识分享出来,希望能对初学者有所帮助.另一方面总结自己所学,以备以后查看. ...

  4. js购物时的放大镜效果

    首先需要两张一样的图片,一张大图,一张小图,大图显示,当鼠标移入时,小图上出现一个滑块,可以滑动,大图也跟着显示,大图的显示区域和小图一样,当滑块滑到不同的位置,大图显示不同的区域,当鼠标移出时,滑块 ...

  5. 数据操作So easy-LINQ解析

    1.LINQ是什么? LINQ是Language Integrated Query的缩写,即“语言集成查询”的意思.LINQ的提出就是为了提供一种跨越各种数据源的统一的查询方式,它主要包含4个组件-- ...

  6. ios专题 - 异步下载加下载进度显示

    [罗国强原创] 今天被刺激了,愤概地要写下这边博文. 说到http异步下载,首先要知道其中的关键类. 关键类是NSURLConnection  NSURLRequest NSMutableURLReq ...

  7. webstorm的默认project编码为系统编码GBK.

    使用新的IDE,而不会设置,会给你带来灾难. 如下为我是用webstorm时遇到的文件编码问题. 纳闷很久,终于发现是IDE的设置问题. 参考

  8. 暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  9. RD / RMDIR Command

    Quote from: http://ss64.com/nt/rd.html RD Delete folder(s) Syntax RD pathname RD /S pathname RD /S / ...

  10. Stay Hungry, Stay Foolish--2005斯坦福大学05年毕业演讲

    转自http://www.cnblogs.com/daizhj/articles/1493813.html 斯蒂夫•保罗•乔布斯(Steve Paul Jobs,1955年2月24日出生-)是蘋果電腦 ...