## [ 传送门 ]()

Description 

Let \((1+\sqrt2)^n=e(n)+f(n)\cdot\sqrt2\) , both \(e(n)\) and \(f(n)\) are integers

Let \(g(n)\) be the gcd of \(f(1),f(2),...,f(n)\)

given \(n\), \(p\), where \(p\) is a prime number

Calculate the value of

\[ \sum_{i=1}^{n}i\cdot g(i) \ \ \ \ mod \ p
\]

\(T\leq 210 ,\sum n\leq 3×10^6\)

Solution 

\[f(n)=2f(n-1)+f(n-2),f(0)=1,f(1)=1
\]

Similar to the \(Fibonacci\) sequence, we have

\[ gcd(f(a),f(b))=f(gcd(a,b))
\]

It's hard to evaluate LCM directly,but we can get it by maximum inversion

\[ lcm(S)=\prod_{T⊆S,T≠∅}gcd(T)^{(−1)^{|T|−1}}
\]

so we can find that

\[ g(n)=\prod_{T⊆S,T≠∅}f(gcd(T))^{(−1)^{|T|−1}}
\]

The next step is the most important.

construct a function \(h\) ,which satisfies

\[ f(n)=\prod_{d|n}h(d)
\]

we can calculate \(h(1...n)\) easily by \(O(n\log n)\)

and

\[ \begin{equation}
 \begin{split}
 g(n)&=\prod_{d=1}^n h(d)^{∑_{T⊆S,T≠∅,d|gcd(T)}(−1)^{|T|+1}}\\
 &=\prod_{d=1}^nh(d)
 \end{split}
 \end{equation}
\]

Code 

#include<bits/stdc++.h>
#define ll long long
#define reg register
#define db double
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
const int MN=1e6+5;
int f[MN],h[MN],n,P,ans;
int Mul(int x,int y){return 1ll*x*y%P;}
int Add(int x,int y){return (x+y)%P;}
int fpow(int x){int r=1,m=P-2;for(;m;m>>=1,x=Mul(x,x))if(m&1)r=Mul(r,x);return r;}
int main()
{
int T=read();
while(T--)
{
n=read(),P=read();
reg int i;
f[0]=0;h[0]=h[1]=f[1]=1;
for(i=2;i<=n;++i) h[i]=1,f[i]=Add(Mul(f[i-1],2),f[i-2]);
for(i=2;i<=n;++i)
{
h[i]=Mul(f[i],fpow(h[i]));
for(int j=i<<1;j<=n;j+=i) h[j]=Mul(h[j],h[i]);
}
for(ans=0,i=1;i<=n;++i) h[i]=Mul(h[i],h[i-1]),ans=Add(ans,Mul(h[i],i));
printf("%d\n",ans);
}
return 0;
}

Blog来自PaperCloud,未经允许,请勿转载,TKS!

最新文章

  1. JavaScript数组的reduce方法详解
  2. Javascript获取随机数
  3. Consuming a RESTful Web Service
  4. js日期计算及快速获取周、月、季度起止日,获取指定日期周数以及星期几的小例子
  5. HTML图片元素(标记)
  6. 微信第一个“小程序”亮相:不是APP胜似APP!
  7. Java for LeetCode 026 Remove Duplicates from Sorted Array
  8. Dlib is a modern C++ toolkit(非常全面的类库)
  9. 最小费用最大流 POJ2195-Going Home
  10. [Everyday Mathematics]20150128
  11. [stack]Evaluate Reverse Polish Notation
  12. JAVA小知识点-Finally和Return的执行关系
  13. SQL SERVER与C#的数据类型对应表
  14. mysql存入中文乱码问题
  15. Supercomputer 解题报告
  16. [FJOI2018]领导集团问题 mulitset合并
  17. 20165232 预备作业3 Linux安装及学习
  18. Python打包项目为EXE程序
  19. Redis和Memcached对比【转】
  20. (转+整理)C#中动态执行代码

热门文章

  1. SQLServer更新数据库每行一个随机数
  2. Deployment.spec.selector.matchLables实验解释
  3. Kafka 生产者、消费者与分区的关系
  4. C++字符串相互转换
  5. 【开发工具】- 设置Sublime支持韩文展示
  6. JavaScript之鼠标事件
  7. 原油petrolaeum石油 Archaic spelling of petroleum
  8. 【python】文件下载---基础版
  9. 大数据之kafka-02.搞定kafka专业术语
  10. PHP生成小程序二维码