CodeForces258C Little Elephant and LCM(数学推理)
来源:互联网 发布:windows vue webpack 编辑:程序博客网 时间:2024/06/06 23:56
题目描述:点击打开链接
题目给你一个a数列,你要求一个b数列满足以下条件:1,b数列中的数不大于对应位置的a数列中的数。2,b数列的最大值要等于b数列中所有的最小公倍数。求满足条件的b数列的数目。首先考虑如果b数列的最大数等于b中所有数的lcm的话,那么除了这个最大的数,剩下所有的数都是这个最大数的因子。再者这个题你改变a数列的顺序是不改变答案的。所以我们可以先把a排序,找到最大的数max,这就是b数列中最大数的最大值,然后开始枚举从max到1,对于每种情况下的答案怎么计算其实也不难,先把此时的最大值因子分解,对于每个因子用二分找到a数列中第一个大于等于该因子的位置。这么做的目的举个例子就知道,假设a数列为1,2,3,6,8现在我们令b数列的最大值为8,8的因子为1,2,4,8我们二分找到第一个大于等于1的位置为1,那么就说明前1个数能够选择的数只有1,对于2我们找到位置2,由于之前已经算过第一个数只能为1,那么用现在的位置2-1,就表示有1个数可以选择1,2两个数,同样后面的位置也这样计算,就可以算出每个位置能够选择的数有多少个,那么累乘一下其实就是方案总数。需要特殊处理的是最后几个位置,假设我们存在3个位置可以选择所有的数包括最大数的话,由于我们要保证最大值,所以我们不能直接累乘最后三个位置的方案数,具体的代码中体现需要自己思考一下。
AC代码:
#include<iostream>#include<sstream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<stack>#include<queue>#include<algorithm>using namespace std;const long long MOD=1e9+7;const int MAXM=1e5;long long a[MAXM+10];long long b[MAXM+10];long long cnt;bool cmp(long long x,long long y){ return x>y;}void handle(long long x){ cnt=0; for (long long i=1;i<=(long long)sqrt(x);i++) if (x%i==0) { b[cnt++]=i; if (i*i!=x) b[cnt++]=x/i; } sort(b,b+cnt);}long long pow(long long x,long long y){ long long res=1; while(y) { if (y&1) res=(res*x)%MOD; x=(x*x)%MOD; y=y>>1; } return res;}int main(){ long long n;// init(); scanf("%lld",&n); for (int i=1;i<=n;i++) scanf("%lld",&a[i]); sort(a+1,a+1+n); long long ans=0; for (long long i=a[n];i>=1;i--) { long long x=i; handle(x); long long tmp=1; long long pre=0; for (long long j=1;j<cnt;j++) { int index=lower_bound(a+1,a+1+n,b[j])-(a+1); long long sum=(long long)index; tmp=tmp*pow(j,sum-pre)%MOD; pre=sum; } tmp=tmp*(pow(cnt,n-pre)-pow(cnt-1,n-pre)+MOD)%MOD; ans=(ans+tmp)%MOD; // ans--; } printf("%lld\n",ans%MOD); return 0;}
阅读全文
0 0
- CodeForces258C Little Elephant and LCM(数学推理)
- 【数论】[CF258C]Little elephant and LCM
- Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)
- Codeforces Round #157 (Div. 1), problem: (C) Little Elephant and LCM DP+组合数学 0(n^(5/2))
- Codeforces-258C:Little Elephant and LCM(数论)
- codeforces 259-B. Little Elephant and Magic Square(数学)
- Little Elephant and Function
- Little Elephant and Problem
- Little Elephant and Mouses
- Little Elephant and Interval
- <codeforces>Little Elephant and Sorting
- A. Little Elephant and Function
- A. Little Elephant and Chess
- A. Little Elephant and Rozdil
- B. Little Elephant and Numbers
- Little Elephant and Inversions CF220E
- A. Little Elephant and Bits
- B. Little Elephant and Sorting
- C++11多线程的创建
- CSS3 @font-face
- markdown编辑器
- MySQL用户管理
- 面试问题(请说出除微服务以外的其他架构模式,以及可选的技术有哪些)
- CodeForces258C Little Elephant and LCM(数学推理)
- 网络的管理
- 蓝桥杯 01背包
- 简单素数表
- Gym 101246.C
- Java操作MongoDB
- apache查看工作模式及调优
- Python 包相关安装情况
- LeetCode-112. Path Sum