TrickGCD hdu6053
来源:互联网 发布:php app接口开发参数 编辑:程序博客网 时间:2024/05/22 12:09
TrickGCD
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 answermod 109+7
Sample Input
144 4 4 4
Sample Output
Case #1: 17
题意:
给一个长度为n的数组A,让你构造等长的数组B,B数组中的元素取值为小于等于A数组中对应位置的元素,现在询问B数组中的gcd大于等于2的方案数.
分析:
找到
容斥的方法:莫比乌斯函数。
考虑一个数n,假设他能分解成三个质因数相乘:a*b*c,那么在算gcd为a、b、c时分别算了一遍,所以我们要减去gcd为a*b、a*c、b*c的,再加上gcd为a*b*c的。推广:我们需要加上质因数为奇数个的,减去质因数为偶数个的。当某个质因数的指数大于等于2,那么这个数我们之前肯定算过,所以这个不计入最后答案。
而莫比乌斯函数正好符合我们的需要(取个相反数就ok了)。
但是这样肯定会超时,所以我们可以进行一下优化,把式子化简为
就是把枚举n个
AC代码如下:
#include <iostream>#include <cstdio>#include <queue>#include <string.h>#include <algorithm>#define inf 0x3f3f3f3fusing namespace std;const int mod = 1e9+7;const int maxn = 100200;bool isprime[maxn];///1代表是素数int prime[maxn];int miu[maxn];///存储的是莫比乌斯函数的值void moblus()///莫比乌斯函数值的打表{ int cnt = 0;///记录素数的个数 miu[1] = 1; memset(isprime,1,sizeof(isprime)); for(long long i = 2; i < maxn; i++) { if(isprime[i]) { prime[cnt++] = i; miu[i] = -1; } for(long long j = 0; j < cnt && i*prime[j] < maxn; j++) { isprime[i*prime[j]] = 0;///0代表不是素数 if(i % prime[j])///比如i=5,prime[0]=2,prime[1]=3,prime[3]=5;miu[5*2]=1,miu[5*2*3]=1; miu[i*prime[j]] = -miu[i]; else { miu[i*prime[j]] = 0; break; } } }}long long quick_mod(long long a,long long b)///快速幂取模{ long long ans = 1; while(b) { if(b&1) { ans=(ans*a)%mod; b--; } b>>=1; a=(a*a)%mod; } return ans;}int a[102000];///下标代表输入的数,值代表这个数出现的次数int suma[202000];///累加的和int main(){ int cas,ncase = 0; int n,v; int maxa,mina; scanf("%d",&cas); moblus(); while(cas--) { ncase++; long long ans = 0;///方案数 maxa = -100200,mina = 102000; scanf("%d",&n); memset(a,0,sizeof(a));///初始化 for(int i = 1; i <= n; i++) { scanf("%d",&v); a[v]++; mina = min(mina,v);///a数组的最小值 maxa = max(maxa,v);///a数组中的最大值 } for(int i = 1; i <= maxa*2; i++)///求出这个数组的前缀和 { suma[i] = suma[i-1] + a[i]; } long long tempans = 1; for(int i = 2; i <= mina; i++)///从2依次枚举 { tempans = 1; for(int j = 1; j*i <= maxa; j++) { tempans *= quick_mod(j,(suma[i*j+i-1]-suma[i*j-1])); tempans %= mod; } ans -= miu[i]*tempans; ans %= mod; } printf("Case #%d: %lld\n",ncase,(ans+mod)%mod); }}
阅读全文
0 0
- HDU6053-TrickGCD
- hdu6053 TrickGCD
- HDU6053-TrickGCD
- HDU6053 TrickGCD
- TrickGCD hdu6053
- [数论 反演] HDU6053. TrickGCD
- HDU6053 TrickGCD【数学】
- HDU6053 TrickGCD(容斥原理)
- 【HDU6053】TrickGCD(莫比乌斯容斥)
- HDU6053 TrickGCD(2017多校第2场)
- hdu6053 TrickGCD 莫比乌斯反演
- HDU6053-TrickGCD 容斥原理+莫比乌斯反演
- 【2017多校】HDU6053 TrickGCD 【莫比乌斯】
- hdu6053 TrickGCD 莫比乌斯函数 容斥原理
- 2017多校训练Contest2: 1009 TrickGCD hdu6053
- 2017杭电多校联赛第二场-TrickGCD (hdu6053)莫比乌斯容斥
- [莫比乌斯函数][分段] hdu6053 TrickGCD (2017 Multi-University Training Contest
- [莫比乌斯反演] HDU6053: [2017 多校-第2场] TrickGCD
- OpenCV学习——对富特征进行光流匹配
- 腾讯libco实现原理分析
- java(6)数组
- 史上最全最丰富的“最长公共子序列”、“最长公共子串”问题的解法与思路
- HightCharts应用实例
- TrickGCD hdu6053
- java 方法重写和重载
- C#获取系统硬盘唯一ID,排除U盘
- c# 温湿度变送器传感器开发(二)
- 代码追查并解释:其他704下载用户数统计
- linux的用户管理
- golang中的websocket实现
- SSH整合Spring、Struts、Hibernate、web配置文件
- 欢迎使用CSDN-markdown编辑器