TrickGCD
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1218 Accepted Submission(s): 464
Problem Description
You are given an array
A
, and Zhu wants to know there are how many different array
B
satisfy the following conditions?
* 1≤Bi≤Ai
* For each pair( l , r ) ( 1≤l≤r≤n ) , gcd(bl,bl+1...br)≥2
* 1≤Bi≤Ai
* For each pair( l , r ) ( 1≤l≤r≤n ) , gcd(bl,bl+1...br)≥2
Input
The first line is an integer T(
1≤T≤10
) describe the number of test cases.
Each test case begins with an integer number n describe the size of array A .
Then a line contains n numbers describe each element of A
You can assume that 1≤n,Ai≤105
Each test case begins with an integer number n describe the size of array A .
Then a line contains n numbers describe each element of A
You can assume that 1≤n,Ai≤105
Output
For the
k
th test case , first output "Case #k: " , then output an integer as answer in a single line . because the answer may be large , so you are only need to output answer
mod
109+7
Sample Input
1 4 4 4 4 4
Sample Output
Case #1: 17
Source
—————————————————————————————————
题意:给你n个数字,每个位置的数字可以小于等于a[i],求所有gcd(l,r)都满足大于等于2的情况数
解题思路:枚举gcd的情况,每种gcd的情况等于所有a[i]/gcd的乘积,但这显然会超,所以需要优化,我们可以分块处理,如枚举5时,5 6 7 8 9对应的都是1个,所以我们可以按gcd分块,而且越大块越少,快内用快速幂加速。 得到一个dp数组dp[i]表示gcd为i的方案数,最后容斥搞一搞
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <cstring>
- #include <string>
- #include <queue>
- #include <stack>
- #include <set>
- #include <map>
- using namespace std;
- #define LL long long
- const LL mod=1e9+7;
- const int INF=0x3f3f3f3f;
- #define MAXN 100005
- int pre[MAXN];
- LL a[MAXN];
- LL dp[MAXN];
- LL qpow(LL a,LL b)
- {
- LL ans=1;
- while(b)
- {
- if(b&1) ans=(ans*a)%mod;
- b>>=1,a=(a*a)%mod;
- }
- return ans;
- }
- int main()
- {
- int T,n;
- int q=1;
- for(scanf("%d",&T); T--;)
- {
- scanf("%d",&n);
- memset(pre,0,sizeof pre);
- for(int i=0; i<n; i++)
- {
- scanf("%lld",&a[i]);
- pre[a[i]]++;
- }
- for(int i=1; i<MAXN; i++) pre[i]+=pre[i-1];
- for(int i=2; i<MAXN; i++)
- {
- dp[i]=1LL;
- for(int j=0; j<MAXN; j+=i)
- {
- int cnt;
- if (j == 0) cnt = pre[j + i - 1];
- else if (j + i - 1 > 100000) cnt = pre[100001] - pre[j - 1];
- else cnt=pre[j+i-1]-pre[j-1];
- if(j/i==0&&cnt) {dp[i]=0;break;}
- dp[i]=(dp[i]*qpow(j/i,(LL)cnt))%mod;
- }
- }
- LL ans=0;
- for(int i=a[n-1]; i>1; i--)
- {
- for(int j=i+i; j<=a[n-1]; j+=i)
- {
- dp[i]-=dp[j];
- dp[i]=(dp[i]%mod+mod)%mod;
- }
- ans+=dp[i];
- ans%=mod;
- }
- printf("Case #%d: %lld\n",q++,ans);
- }
- return 0;
- }
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define LL long long
const LL mod=1e9+7;
const int INF=0x3f3f3f3f;
#define MAXN 100005
int pre[MAXN];
LL a[MAXN];
LL dp[MAXN];
LL qpow(LL a,LL b)
{
LL ans=1;
while(b)
{
if(b&1) ans=(ans*a)%mod;
b>>=1,a=(a*a)%mod;
}
return ans;
}
int main()
{
int T,n;
int q=1;
for(scanf("%d",&T); T--;)
{
scanf("%d",&n);
memset(pre,0,sizeof pre);
for(int i=0; i<n; i++)
{
scanf("%lld",&a[i]);
pre[a[i]]++;
}
for(int i=1; i<MAXN; i++) pre[i]+=pre[i-1];
for(int i=2; i<MAXN; i++)
{
dp[i]=1LL;
for(int j=0; j<MAXN; j+=i)
{
int cnt;
if (j == 0) cnt = pre[j + i - 1];
else if (j + i - 1 > 100000) cnt = pre[100001] - pre[j - 1];
else cnt=pre[j+i-1]-pre[j-1];
if(j/i==0&&cnt) {dp[i]=0;break;}
dp[i]=(dp[i]*qpow(j/i,(LL)cnt))%mod;
}
}
LL ans=0;
for(int i=a[n-1]; i>1; i--)
{
for(int j=i+i; j<=a[n-1]; j+=i)
{
dp[i]-=dp[j];
dp[i]=(dp[i]%mod+mod)%mod;
}
ans+=dp[i];
ans%=mod;
}
printf("Case #%d: %lld\n",q++,ans);
}
return 0;
}