题目链接

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 kth 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

题意:

给出长度为n的A数列,求满足条件的B数组的个数,条件:①1<=b[i]<=a[i] ②对于任意区间【L,R】,区间gcd>=2

看网上大神的代码都是用莫比乌斯反演来求解的,不大理解这个,就说一下我自己的一个思路吧。

定义:

dp[i]表示gcd为i的数的个数, 则b中每个元素都为i的倍数

a数组保存每一个输进去的值,cnt[i]表示小于等于i的数的个数

设d为当前的gcd

b[i]<=a[i] 则第i个位置有a[i]/d种选择 直接累乘TLE.

若a[i]/d=k贡献为k,则和它相同贡献有cnt[kd,(k+1)d-1]个,则按段来枚举,算出该段贡献k^cnt.

最后容斥减掉gcd为jx的部分(j>1)

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N=2e5+20;
  5. const ll mod=1e9+7;
  6. ll dp[N],n,a[N],cnt[N];///dp[i]表示gcd为i的数的个数,a数组保存每一个输进去的值,cnt[i]表示小于等于i的数的个数
  7. ll powmod(ll x,ll n)///快速幂求出x^n取模后的结果
  8. {
  9. ll s=1;
  10. while(n)
  11. {
  12. if(n&1)
  13. s=(s*x)%mod;
  14. n>>=1;
  15. x=(x*x)%mod;
  16. }
  17. return s%mod;
  18. }
  19. int main()
  20. {
  21. int T;
  22. scanf("%d",&T);
  23. int cas=0;
  24. while(T--)
  25. {
  26. scanf("%d",&n);
  27. memset(cnt,0,sizeof(cnt));
  28. memset(dp,0,sizeof(dp));
  29. ll mx=0;
  30. for(int i=1; i<=n; i++)
  31. {
  32. scanf("%lld",&a[i]);
  33. mx=max(mx,a[i]);///mx表示输进去的这些数的最小值
  34. cnt[a[i]]++;
  35. }
  36. for(int i=1; i<=mx; i++)
  37. cnt[i]+=cnt[i-1];///cnt最终表示的是小于等于i的数的个数
  38. ll ans=0;
  39. for(int i=mx; i>=2; i--)///当前是以i为gcd
  40. {
  41. ll res=1;
  42. if(cnt[i-1])///压根就不存在比i小的数,那么也不可能有以i为gcd的数
  43. {
  44. dp[i]=0;
  45. continue;
  46. }
  47. for(int j=i; j<=mx; j+=i)///j都是i的倍数
  48. {
  49. ll num=cnt[min(mx,(ll)j+i-1)]-cnt[j-1];//[ki~(k+1)i),在这个标准下的输的个数
  50. ll x=j/i;///贡献为x
  51. if(num)
  52. res=(res*powmod(x,num))%mod;///这里的含义可以理解为每个数都有x种选法,现在一共有num个数,则应该是x^num
  53. }
  54. dp[i]=res;
  55. }
  56. for(int i=mx; i>=2; i--)
  57. {
  58. for(int j=i+i; j<=mx; j+=i)
  59. dp[i]=(dp[i]-dp[j]+mod)%mod;
  60. ans=(ans+dp[i])%mod;
  61. }
  62. printf("Case #%d: %lld\n",++cas,ans);
  63. }
  64. return 0;
  65. }

2017ACM暑期多校联合训练 - Team 2 1009 HDU 60563 TrickGCD (容斥公式)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)

    题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...

  2. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  3. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  4. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  5. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  6. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  7. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  8. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  9. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

随机推荐

  1. CentOS7 修改yum源为阿里云

    1,登陆root帐号 2,cd /etc/yum.repo.d 3,mv CentOS-Base.repo CentOS-Base.repo.bak4,wget http://mirrors.aliy ...

  2. Spring-MVC理解之二:前置控制器

    原文链接:http://www.cnblogs.com/brolanda/p/4265749.html 一.前置控制器配置与讲解 上篇中理解了IOC容器的初始化时机,并理解了webApplicatio ...

  3. Android Studio- 把项目提交到SVN中操作方法

    第一步 下载SVN,下载完成之后,需要吧command line client tools点击修改安装 然后Crash Reporter点击选择取消安装 如果不进行该操作,则可能在C:\Program ...

  4. BZOJ5073 小A的咒语(动态规划)

    设f[i][j][0/1]为前i位选j段时其中第i位选/不选最多能匹配到哪,转移时f[i][j][0]→f[i+1][j][0],f[i][j][1]→f[i+1][j][0],f[i][j][1]→ ...

  5. Luogu4926 倍杀测量者(二分答案+差分约束)

    容易想到二分答案.问题变为判断是否所有条件都被满足,可以发现这是很多变量间的相对关系,取个log之后就是经典的差分约束模型了.特殊的地方在于某些人的分数已被给定,从每个人开始跑一遍最短路判断一下是否能 ...

  6. P1107 [BJWC2008]雷涛的小猫

    题目描述 雷涛同学非常的有爱心,在他的宿舍里,养着一只因为受伤被救助的小猫(当然,这样的行为是违反学生宿舍管理条例的).在他的照顾下,小猫很快恢复了健康,并且愈发的活泼可爱了. 可是有一天,雷涛下课回 ...

  7. P2573 [SCOI2012]滑雪

    题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着 M 条供滑行的轨道和 N 个轨道之间的交点(同时也是景点),而且每个景点都有一编号 i ( 1≤i≤N )和一高度 Hi.a18028 ...

  8. Python清理过期文件

    改程序执行后,会清理 test/文件夹中距离现在超过一天的以 .xml 结尾的文件 # coding: utf-8 import time import os root = os.path.dirna ...

  9. EVE-NG硬盘扩容,存储海量镜像

    EVE-NG硬盘扩容,存储海量镜像 来源 http://blog.51cto.com/sms1107/1928453 一.查看当前磁盘使用情况 /dev/mapper/eve--ng--vg-root ...

  10. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...