生活随笔
收集整理的這篇文章主要介紹了
[bzoj4833][数论][min-max容斥]最小公倍佩尔数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Description
令(1+sqrt(2))n=e(n)+f(n)*sqrt(2),其中e(n),f(n)都是整數,顯然有(1-sqrt(2))n=e(n)-f(n)*sqrt(2)。令g(
n)表示f(1),f(2)…f(n)的最小公倍數,給定兩個正整數n和p,其中p是質數,并且保證f(1),f(2)…f(n)在模p意義
下均不為0,請計算sigma(i*g(i)),1<=i<=n.其在模p的值。
Input
第一行包含一個正整數 T ,表示有 T 組數據,滿足 T≤210 。接下來是測試數據。每組測試數據只占一行,包含 兩個正整數 n 和 p
,滿足 1≤n≤106,2≤p≤109+7 。保證所有測試數據的 n 之和不超過 3×10^6 。
Output
對于每組測試數據,輸出一行一個非負整數,表示這組數據的答案。
Sample Input
5
1 233
2 233
3 233
4 233
5 233
Sample Output
1
5
35
42
121
題解
推一下式子
會有e(n+1)=e(n)+2?f(n)e(n+1)=e(n)+2*f(n)e(n+1)=e(n)+2?f(n),f(n+1)=e(n)+f(n)f(n+1)=e(n)+f(n)f(n+1)=e(n)+f(n)
所以e(n+1)=f(n)+f(n+1)e(n+1)=f(n)+f(n+1)e(n+1)=f(n)+f(n+1)
所以f(n+1)=2?f(n)+f(n?1)f(n+1)=2*f(n)+f(n-1)f(n+1)=2?f(n)+f(n?1)
類似斐波那契數列的式子,結論有一個是形如f(n+1)=a?f(n)+b?f(n?1)f(n+1)=a*f(n)+b*f(n-1)f(n+1)=a?f(n)+b?f(n?1)的式子均滿足gcd(f(a),f(b))=f(gcd(a,b))gcd(f(a),f(b))=f(gcd(a,b))gcd(f(a),f(b))=f(gcd(a,b))
那么接下來就和51nod1355是一樣的了
感覺直接抄上去就能過啊…
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
using namespace std
;
inline int read()
{int f
=1,x
=0;char ch
=getchar();while(ch
<'0'||ch
>'9'){if(ch
=='-')f
=-1;ch
=getchar();}while(ch
>='0'&&ch
<='9'){x
=x
*10+ch
-'0';ch
=getchar();}return x
*f
;
}
int stack
[20];
inline void write(LL x
)
{if(x
<0){putchar('-');x
=-x
;}if(!x
){putchar('0');return;}int top
=0;while(x
)stack
[++top
]=x
%10,x
/=10;while(top
)putchar(stack
[top
--]+'0');
}
inline void pr1(int x
){write(x
);putchar(' ');}
inline void pr2(LL x
){write(x
);putchar('\n');}
const int MAXN
=3000005;
LL f
[MAXN
],g
[MAXN
],ans
,mod
;
LL
pow_mod(LL a
,LL b
)
{LL ret
=1;while(b
){if(b
&1)ret
=ret
*a
%mod
;a
=a
*a
%mod
;b
>>=1;}return ret
;
}
int n
;
int main()
{int T
=read();while(T
--){n
=read();mod
=read();f
[1]=g
[1]=1;for(int i
=2;i
<=n
;i
++)f
[i
]=g
[i
]=(2*f
[i
-1]+f
[i
-2])%mod
;for(int i
=1;i
<=n
;i
++){LL inv
=pow_mod(g
[i
],mod
-2);for(int j
=2;i
*j
<=n
;j
++)g
[i
*j
]=g
[i
*j
]*inv
%mod
;}ans
=0;LL temp
=1;for(int i
=1;i
<=n
;i
++){temp
=temp
*g
[i
]%mod
;ans
=(ans
+temp
*i
)%mod
;}pr2(ans
);}return 0;
}
總結
以上是生活随笔為你收集整理的[bzoj4833][数论][min-max容斥]最小公倍佩尔数的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。