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?
*1≤Bi≤Ai
* For each pair( l , r ) (1≤l≤r≤n ) , gcd(bl,bl+1...br)≥2
*
* For each pair( l , r ) (
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 arrayA .
Then a line containsn numbers describe each element of A
You can assume that1≤n,Ai≤105
Each test case begins with an integer number n describe the size of array
Then a line contains
You can assume that
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
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的方案数,最后容斥搞一搞
- #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;
- }
阅读全文
0 0
- hdu 6053
- 【HDU】6053
- hdu 6053
- hdu 6053
- HDU 6053 TrickGCD
- HDU 6053 TrickGCD
- HDU-6053 TrickGCD
- hdu 6053 TrickGCD
- HDU 6053 TrickGCD
- [HDU]6053 TrickGCD
- HDU 6053 TrickGCD 数论
- hdu--6053--TrickGCD
- hdu-6053TrickGCD
- HDU 6053 TrickGCD
- HDU 6053 TrickGCD
- hdu
- hdu
- HDU
- 单一职责原则
- 2017理数全国卷I T21
- C++ 域名转IP地址
- C# winform PictureBox插入图片并设置自适应显示
- 双边滤波原理理解
- hdu 6053
- hive随笔
- C语言 快速排序
- iOS 生成自定义位数随机数
- HDU1892
- log4j.properties 详解与配置步骤
- #NOIP模拟赛#保证的利润(贪心 模拟)
- Spring报Context namespace element 'component-scan'... are only available on JDK 1.5 and higher解决问题
- 插值器