hdu--6053--TrickGCD
来源:互联网 发布:app交互设计软件 编辑:程序博客网 时间:2024/05/20 03:04
TrickGCD
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2575 Accepted Submission(s): 980
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
官方题解:
令F(i)表示是i倍数的方案数,可以容易的通过预处理出前缀和后nlogn的时间内求出,之后利用预处理出莫比乌斯函数后进行简单的反演即可算出答案,加上快速幂,总复杂度nlogn^2
我的思路:
因为不会用莫比乌斯,所以用了容斥原理和筛选。首先看到这道题,正常的思维是枚举所有gcd的值,每个位置都有a[i]/gcd个数可以选择,然后再进行累积
就能得出答案, 但这明显的需要优化,我们枚举除数,把a[i]/gcd的结果相同的·放到一块,举个例子: 比如gcd为10,则[20,30)这个范围内的数除以10,都为2,那么这个
范围内的数就可以放到一块算,如果这个范围内的数一共有n个,那么就用快速幂算q_pow(10,n);num[i]表示gcd为i的方案数,因为这样算的有重复的,所以后面还需要进
容斥,用pd[i]表示容斥后的方案数。
代码:
C++ Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
const long long mod = 1e9 + 7;
typedef long long LL;
int n;
int a[MAXN];///a数组统计每个数的个数
int sum[MAXN];///前缀和
LL num[MAXN];///num[i]表示数列gcd为i时的的累积
LL dp[MAXN];///dp[i]表示容斥完之后也就是最终结果 gcd为i时的累积
///整数快速幂
LL quick_pow(int a, int b)
{
long long sum = 1, base = a;
while(b)
{
if(b & 1)
sum = sum * base % mod;
base = base * base % mod;
b >>= 1;
}
return sum;
}
int main()
{
int t, mi;
scanf("%d", &t);
int ca = 0;
while(t--)
{
mi = MAXN;
int x;
scanf("%d", &n);
memset(a, 0, sizeof a);
for(int i = 0; i < n; ++i)
{
scanf("%d", &x);
mi = min(mi, x);
a[x]++;
}
memset(sum, 0, sizeof sum);
for(int i = 1; i <= 100000; ++i)
sum[i] = sum[i - 1] + a[i];
for(int i = 2; i <= mi; ++i)///枚举gcd
{
num[i] = 1;
for(int j = 0; j <= 100000; j += i)
{
int b = sum[min(j + i - 1, 100000)] - sum[max(j - 1, 0)]; ///注意边界
int a = j / i;
num[i] = num[i] * quick_pow(a, b) % mod;
}
}
for(int i = 100000; i >= 2; --i)
{
dp[i] = num[i];
for(int j = i * 2; j <= 100000; j += i)
{
dp[i] = (dp[i] - dp[j] + mod) % mod;
}
}
LL ans = 0;
for(int i = 2; i <= 100000; ++i)
ans = (ans + dp[i]) % mod;
printf("Case #%d: %I64d\n", ++ca, ans);
}
return 0;
}
using namespace std;
const int MAXN = 1e5 + 7;
const long long mod = 1e9 + 7;
typedef long long LL;
int n;
int a[MAXN];///a数组统计每个数的个数
int sum[MAXN];///前缀和
LL num[MAXN];///num[i]表示数列gcd为i时的的累积
LL dp[MAXN];///dp[i]表示容斥完之后也就是最终结果 gcd为i时的累积
///整数快速幂
LL quick_pow(int a, int b)
{
long long sum = 1, base = a;
while(b)
{
if(b & 1)
sum = sum * base % mod;
base = base * base % mod;
b >>= 1;
}
return sum;
}
int main()
{
int t, mi;
scanf("%d", &t);
int ca = 0;
while(t--)
{
mi = MAXN;
int x;
scanf("%d", &n);
memset(a, 0, sizeof a);
for(int i = 0; i < n; ++i)
{
scanf("%d", &x);
mi = min(mi, x);
a[x]++;
}
memset(sum, 0, sizeof sum);
for(int i = 1; i <= 100000; ++i)
sum[i] = sum[i - 1] + a[i];
for(int i = 2; i <= mi; ++i)///枚举gcd
{
num[i] = 1;
for(int j = 0; j <= 100000; j += i)
{
int b = sum[min(j + i - 1, 100000)] - sum[max(j - 1, 0)]; ///注意边界
int a = j / i;
num[i] = num[i] * quick_pow(a, b) % mod;
}
}
for(int i = 100000; i >= 2; --i)
{
dp[i] = num[i];
for(int j = i * 2; j <= 100000; j += i)
{
dp[i] = (dp[i] - dp[j] + mod) % mod;
}
}
LL ans = 0;
for(int i = 2; i <= 100000; ++i)
ans = (ans + dp[i]) % mod;
printf("Case #%d: %I64d\n", ++ca, ans);
}
return 0;
}
阅读全文
0 0
- 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 6053 TrickGCD DP(筛法)
- HDU 6053 TrickGCD(枚举)
- TrickGCD HDU
- TrickGCD HDU
- hdu 6053 TrickGCD(筛法+容斥)
- hdu 多校 TrickGCD
- HDU 6503 TrickGCD
- 分享一篇JAVA成长之路的经验篇
- poj 2195 going home二分图最大匹配
- 根据秒数来描述年月日
- GCD
- 在 CentOS7 上安装 Zookeeper-3.4.9 服务
- hdu--6053--TrickGCD
- Hadoop组件
- jmeter 录制回放工具badboy 下载安装步骤
- Til the Cows Come Home (Dijkstra)
- ios-画一个图片水印和文字水印
- 小程序form表单提交
- LintCode 寻找缺失的数 JAVA
- webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】
- GFIC 2017-全球MSO运营商峰会拟定议题