CodeForces
来源:互联网 发布:win10软件卸载不了 编辑:程序博客网 时间:2024/05/29 19:36
Simon has a prime number x and an array of non-negative integers a1, a2, …, an.
Simon loves fractions very much. Today he wrote out number on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + … + an. Now Simon wants to reduce the resulting fraction.
Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).
The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109) — the size of the array and the prime number.
The second line contains n space-separated integers a1, a2, …, an (0 ≤ a1 ≤ a2 ≤ … ≤ an ≤ 109).
Print a single number — the answer to the problem modulo 1000000007 (109 + 7).
2 22 2
8
3 31 2 3
27
2 229 29
73741817
4 50 0 0 0
1
In the first sample . Thus, the answer to the problem is 8.
In the second sample, . The answer to the problem is 27, as 351 = 13·27, 729 = 27·27.
In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.
In the fourth sample . Thus, the answer to the problem is 1.
思路:
用markdown写一次题解试试,在这道题中,上给了两个n(数字个数)和x(一个素数,是下面各个数的底数),接下来有n个数,比如第二组样例:
3 31 2 3
结果是:
27
代码:
#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <iostream>#include <stack>#include <map>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define N (100000+20)#define M 200010#define MOD (1000000000+7)#define inf 0x3f3f3f3f#define LL long longusing namespace std;LL a[100001];LL power(LL a,LL n){ LL s=1; while (n) { if (n&1) s=s*a%MOD; a=a*a%MOD; n>>=1; } return s;}int main(){ LL n; LL x,sum=0; scanf("%lld%lld",&n,&x); for (LL i=1; i<=n; ++i) { scanf("%lld",&a[i]); sum+=a[i];//算出分母的大小 } LL ans=sum-a[n],cnt=0; for (LL k=a[n]; k; k--) { while (n&&a[n]==k) { n--; cnt++;//系数 } if (cnt%x) break; ans++;//算出一共有几项 cnt/=x; } printf("%lld\n",power(x,ans)); return 0;}
顺便贴一个我之前暴力想过的代码,代码应该没错,不过一是long long可能存不下那么大的数,二是我知道要超时,果然在第五个样例的时候超时了:
#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <string>#include <iostream>#include <stack>#include <map>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define N (100000+20)#define M 200010#define MOD (1000000000+7)#define inf 0x3f3f3f3f#define LL long longusing namespace std;LL a[N];LL cifang(LL x,LL y){ LL sum=1; for(LL i=0; i<y; i++) { sum*=(x%MOD); } return (sum%MOD);}LL lcm(LL x,LL y){ return ((x*y)/(__gcd(x,y)))%MOD;}int main(){ LL n,x,l,f1=0,f2=1,maxx=0,f,num; scanf("%lld%lld",&n,&x); for(LL i=0; i<n; i++) { scanf("%lld",&l); f=(cifang(x,l))%MOD; a[i]=f; } for(LL i=0; i<n; i++) f2*=(lcm(a[i],f2))%MOD; for(LL i=0; i<n; i++) f1+=(f2/a[i]); num=__gcd(f1,f2); printf("%lld\n",num%MOD); return 0;}
参考博客:http://blog.csdn.net/hongrock/article/details/14163805
最后再说一下c语言中的define:
c语言#define中要注意,定义
#define MOD (1000000000+7)
的时候要注意加上括号,因为宏定义在替换的时候是直接替换,而并不把MOD的值计算出来,当你取模的时候,编译器会先进行取模运算,最后才把7加上,导致结果错误,所以要注意一下
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- 针对gc 对于java的一些建议 以及四种引用
- (51nod)1284
- WordCount v2.0结合个人理解进行注释
- SQL注入的例子
- 指针使C威力无穷!
- CodeForces
- local function definitions are illegal
- java算法之3Sum
- spark 总结
- android缓存清理
- CSS常见属性
- dskinlite(uieasy)使用交流
- html 不要在未完成的标签内操作此标签
- [iBET Malaysia] Live Casinos 0.75% Rebate Bonus