Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)
2014-11-05 22:53
309 查看
C. Little Elephant and LCMtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The result of the LCM operation of k positiveintegers x1, x2, ..., xk isthe minimum positive integer that is divisible by each of numbers xi.Let's assume that there is a sequence of integers b1, b2, ..., bn.Let's denote their LCMs as lcm(b1, b2, ..., bn) andthe maximum of them as max(b1, b2, ..., bn).The Little Elephant considers a sequence b good, if lcm(b1, b2, ..., bn) = max(b1, b2, ..., bn).The Little Elephant has a sequence of integers a1, a2, ..., an.Help him find the number of good sequences of integers b1, b2, ..., bn,such that for all i (1 ≤ i ≤ n) the followingcondition fulfills: 1 ≤ bi ≤ ai.As the answer can be rather large, print the remainder from dividing it by 1000000007 (109 + 7).InputThe first line contains a single positive integer n (1 ≤ n ≤ 105) —the number of integers in the sequence a. The second line contains nspace-separatedintegers a1, a2, ..., an (1 ≤ ai ≤ 105) —sequence a.OutputIn the single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).Sample test(s)input
4 1 4 3 2output
15input
2 6 3output
13题意:给你一个a序列,找出一个b序列,1 ≤ bi ≤ ai,使得max(bi)=lcm(bi),问这样的bi序列有多少个。思路:先对a排序,枚举i=max(bi),对i因式分解,那么大于等于i的部分很好处理,直接pow_mod()相减,小于i的部分就任意取一个约束就够了。代码:
#include <iostream> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<algorithm> #define INF 0x3f3f3f3f #define maxn 100005 #define mod 1000000007 typedef long long ll; using namespace std; int n; int a[maxn]; ll pow_mod(ll x,ll n) { ll res = 1; while(n) { if(n&1) res = res * x %mod; x = x * x %mod; n >>= 1; } return res; } void solve() { int i,j; ll ans=0,res; sort(a+1,a+n+1); for(i=1;i<=a ;i++) // 枚举答案 { vector<int>fac; for(j=1;j*j<=i;j++) // factor { if(i%j==0) { fac.push_back(j); if(j*j!=i) fac.push_back(i/j); } } sort(fac.begin(),fac.end()); int pos,pre=1; res=1; for(j=1;j<fac.size();j++) { pos=lower_bound(a+1,a+n+1,fac[j])-a; res*=pow_mod(j,pos-pre); res%=mod; pre=pos; } ans+=res*((pow_mod(j,n-pre+1)-pow_mod(j-1,n-pre+1)+mod)%mod); ans%=mod; } printf("%I64d\n",ans); } int main() { int i,j; while(~scanf("%d",&n)) { for(i=1;i<=n;i++) { scanf("%d",&a[i]); } solve(); } return 0; }
题意:
给你一个a序列,找出一个b序列,1 ≤ bi ≤ ai,使得max(bi)=lcm(bi),问这样的bi序列有多少个。
思路:
先对a排序,枚举i=max(bi),对i因式分解,那么大于等于i的部分很好处理,直接pow_mod()相减,小于i的部分就任意取一个约束就够了。
代码:
相关文章推荐
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp,组合数学
- 【转载】【树形DP】【数学期望】Codeforces Round #362 (Div. 2) D.Puzzles
- Codeforces Round #439 (Div. 2)(补题) A模拟+set B 数学 C dp or 杨辉三角组合数
- Topcoder SRM 144 Div1 550(数学和dp问题求方案数,很有意思)
- CF 162-div2 D 数学+dp
- Codeforces Round #178 (Div. 2) :===> DP(01背包类) + 组和数学 + dfs
- Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)
- Codeforces Round #144 (Div. 1), problem: (B) Table DP 组合数学
- Codeforces Round #407 (Div. 2) C 数学 + DP
- Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】
- Codeforces Round #445 Div1 C:Maximum Element (组合数学+DP)
- Codeforces Round #384 (Div. 2) B ,C ,D(规律,数学。树dp)
- Codeforces Round #319 (Div. 2)B. Modulo Sum(数学,DP)
- Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)
- 2017-2018 ACM-ICPC Southeast Regional Contest (Div. 1) J.Treasure Map dp
- 【bzoj3997】【TJOI2015】【组合数学】【dp】
- pku 1191 棋盘分割(DP,一点点数学)
- HDU-4532 湫秋系列故事——安排座位 组合数学DP
- Codeforces Round #274 (Div. 1) B. Long Jumps 数学
- Codeforces Round #280 (Div. 2) E. Vanya and Field 数学