TrickGCDTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 746 Accepted Submission(s): 293
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
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
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
Sample Output
Source
Recommend
|
思路来源:http://www.cnblogs.com/jhz033/p/7246028.html
题意:给你n个数字,每个位置的数字可以小于等于a[i],求所有gcd(l,r)都满足大于等于2的情况数;
思路:显然枚举gcd的情况,对于每个位置都有a[i]/gcd的个数可以满足条件,gcd的情况的所有a[i]/gcd的乘积;
这个也需要优化,枚举除数,a[i]/gcd相同的为一块,nlong(n)的复杂度*快速幂的log,后面的用容斥筛一下就好了;
这个题是向下取整,所以每一块长度就是要除的i本身大小,但如果是向上取整就比较复杂了,下面博客D题有介绍
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 7;
const int Mod = 1e9 + 7;
int a[maxn], ans[maxn], cnt[maxn], num[maxn];
ll quick_mod(ll a, ll b)
{
ll ans = 1;
while(b)
{
if(b&1) ans = ans*a%Mod;
b /= 2;
a = a*a%Mod;
}
return ans;
}
int main()
{
int n, t, ca = 1, x;
scanf("%d", &t);
while(t--)
{
memset(a, 0, sizeof(a));
memset(cnt, 0, sizeof(cnt));
memset(ans, 0, sizeof(ans));
scanf("%d", &n);
for(int i = 1; i <= n; i++) //桶装
{
scanf("%d", &x);
a[x]++;
}
for(int i = 1; i < maxn; i++)
cnt[i] = cnt[i-1]+a[i];
for(int i = 2; i < maxn; i++) //枚举gcd
{
num[i] = 1;
for(int j = 0; j < maxn; j += i) //分段
{
int res, limit = j+i-1 >= maxn ? maxn-1 : j+i-1;
if(!j) res = cnt[limit];
else res = cnt[limit]-cnt[j-1];
int tmp = j/i;
if(!tmp && res) num[i] = 0;
else if(res) num[i] = num[i]*quick_mod(tmp, res)%Mod;
}
}
for(int i = maxn-1; i >= 2; i--) //容斥
{
ans[i] = num[i];
for(int j = i+i; j <= maxn-1; j += i) //容斥
ans[i] = ((ans[i]-ans[j])%Mod+Mod)%Mod;
}
ll res = 0;
for(int i = 2; i <= maxn-1; i++)
res += ans[i], res %= Mod;
printf("Case #%d: %lld\n",ca++,res);
}
return 0;
}
莫比乌斯代码待更新..
TrickGCDTime Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 746 Accepted Submission(s): 293
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
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
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
Sample Output
Source
Recommend
|