hdu 6053

来源:互联网 发布:知乎rss订阅地址 编辑:程序博客网 时间:2024/05/29 05:06

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?

1BiAi
* For each pair( l , r ) (1lrn) , gcd(bl,bl+1...br)2
 

Input
The first line is an integer T(1T10) 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 1n,Ai105
 

Output
For the kth 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
144 4 4 4
 

Sample Output
Case #1: 17
 

Source
2017 Multi-University Training Contest - Team 2
 

—————————————————————————————————

题意:给你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的方案数,最后容斥搞一搞


[cpp] view plain copy
print?
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <algorithm>  
  4. #include <cmath>  
  5. #include <cstring>  
  6. #include <string>  
  7. #include <queue>  
  8. #include <stack>  
  9. #include <set>  
  10. #include <map>  
  11. using namespace std;  
  12. #define LL long long  
  13. const LL mod=1e9+7;  
  14. const int INF=0x3f3f3f3f;  
  15. #define MAXN 100005  
  16.   
  17. int pre[MAXN];  
  18. LL a[MAXN];  
  19. LL dp[MAXN];  
  20.   
  21. LL qpow(LL a,LL b)  
  22. {  
  23.     LL ans=1;  
  24.     while(b)  
  25.     {  
  26.         if(b&1) ans=(ans*a)%mod;  
  27.         b>>=1,a=(a*a)%mod;  
  28.     }  
  29.     return ans;  
  30. }  
  31.   
  32. int main()  
  33. {  
  34.     int T,n;  
  35.     int q=1;  
  36.     for(scanf("%d",&T); T--;)  
  37.     {  
  38.         scanf("%d",&n);  
  39.         memset(pre,0,sizeof pre);  
  40.         for(int i=0; i<n; i++)  
  41.         {  
  42.             scanf("%lld",&a[i]);  
  43.             pre[a[i]]++;  
  44.         }  
  45.         for(int i=1; i<MAXN; i++) pre[i]+=pre[i-1];  
  46.         for(int i=2; i<MAXN; i++)  
  47.         {  
  48.             dp[i]=1LL;  
  49.             for(int j=0; j<MAXN; j+=i)  
  50.             {  
  51.                 int cnt;  
  52.                 if (j == 0) cnt = pre[j + i - 1];  
  53.                 else if (j + i - 1 > 100000) cnt = pre[100001] - pre[j - 1];  
  54.                 else  cnt=pre[j+i-1]-pre[j-1];  
  55.   
  56.                 if(j/i==0&&cnt) {dp[i]=0;break;}  
  57.                 dp[i]=(dp[i]*qpow(j/i,(LL)cnt))%mod;  
  58.             }  
  59.         }  
  60.         LL ans=0;  
  61.         for(int i=a[n-1]; i>1; i--)  
  62.         {  
  63.             for(int j=i+i; j<=a[n-1]; j+=i)  
  64.             {  
  65.                 dp[i]-=dp[j];  
  66.                 dp[i]=(dp[i]%mod+mod)%mod;  
  67.             }  
  68.             ans+=dp[i];  
  69.             ans%=mod;  
  70.         }  
  71.         printf("Case #%d: %lld\n",q++,ans);  
  72.   
  73.     }  
  74.   
  75.     return 0;  
  76. }  


原创粉丝点击