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. 关于NSRunLoop和NSTimer的深入理解

    一.什么是NSRunLoop NSRunLoop是消息机制的处理模式 NSRunLoop的作用在于有事情做的时候使的当前NSRunLoop的线程工作,没有事情做让当前NSRunLoop的线程休眠 NS ...

  2. Android(java)学习笔记209:采用get请求提交数据到服务器(qq登录案例)

    1.GET请求:    组拼url的路径,把提交的数据拼装url的后面,提交给服务器. 缺点:(1)安全性(Android下提交数据组拼隐藏在代码中,不存在安全问题)  (2)长度有限不能超过4K(h ...

  3. php 中json_decode()和json_encode()的使用方法

    1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...

  4. 整理SVN代码-->正式环境的代码

    最近我被分配到了合并正式补丁代码的工作.聊聊整个流程 第一步解压补丁

  5. postgresql行转列并拼接字符串

    有这样一张表: ; id |   kw   ----+--------  1 | big  1 | hello  2 | oracle  2 | small  2 | apple  3 | shit( ...

  6. Asp.net 上传文件小叙(修改FileUpload显示文字等)

    想要在asp.net网站上上传文件就得用到FileUpload,可是这个控件中“浏览”没法修改,可以使用html中<input type="file" 来解决该问题. 首先页 ...

  7. C#中params使用

    1.参数被params修饰即为可变参数,params只能修饰一维数组. 2.给可变参数赋值的时候,可以直接传递数组的元素. 3.在调用的时候,会自动将这些元素封装为一个数组,并将数组传递. 4.可变参 ...

  8. HTML表单元素中disabled的元素的值不会提交到服务器

    一.在HTMl页面的form表单中对disabled的元素的属性和值不会提交到服务器 实例1: <form action="#"> <input type=&qu ...

  9. 20151222jquery学习笔记--验证注册表单

    $(function () { $('#search_button').button({ icons : { primary : 'ui-icon-search', }, }); $('#reg'). ...

  10. Excel对话框大全

    Excel对话框大全 序号 名称 描述 1 Application.Dialogs(1).Show 是调用打开对话框  2 Application.Dialogs(5或145).Show 是调用另存为 ...