TrickGCD
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 697 Accepted Submission(s): 266
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的乘积,这个需要优化,枚举除数,a[i]/gcd相同的为一块(现场的时候没想到这个),然后重复的用容斥搞一下就好了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
const LL mod = 1000000007;
int sum[100009], n;
LL x[100009];
LL qpow(LL x, LL y)
{
LL ans = 1;
while (y)
{
if (y & 1) ans = (ans*x) % mod;
y >>= 1;
x = (x*x) % mod;
}
return ans;
}
int main()
{
int t, cas = 1;
scanf("%d", &t);
while (t--)
{
memset(sum, 0, sizeof sum);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int a;
scanf("%d", &a);
sum[a]++;
}
for (int i = 1; i <= 100005; i++) sum[i] += sum[i - 1];
for (int i = 2; i <= 100000; i++)
{
x[i] = 1LL;
for (int j = 0; j <= 100000; j += i)
{
int a, cnt;
if (j + i - 1 > 100000) cnt = sum[100000] - sum[j - 1];
else if (j == 0) cnt = sum[j + i - 1];
else cnt = sum[j + i - 1] - sum[j - 1];
a = j / i;
if (a == 0 && cnt) x[i] = 0;
else if (cnt) x[i] = (x[i] * qpow(a, cnt)) % mod;
}
}
for (int i = 100000; i >= 2; i--)
{
for (int j = i + i; j <= 100000; j += i)
x[i] -= x[j], x[i] = (x[i] % mod + mod) % mod;
}
LL ans = 0;
for (int i = 2; i <= 100000; i++) ans += x[i], ans %= mod;
printf("Case #%d: %lld\n", cas++, ans);
}
return 0;
}