160 likes | 280 Views
sicily 2501 算算式. _gXX 遇到一个麻烦的式子: S = n1 + n2 + n3 + ...... + nk ,已知 n 、 k ,求 S 的值。 因为 _gXX 数学很差,希望你能告诉他答案。但是由于他的数学实在太差了,所以你只需要告诉他 S 除以 9901 的余数即可。. 费马小定理:假如p是质数,且(a,p)=1,那么 a^(p-1) ≡1(mod p),即假如p是质数,且a,p互质,那么 a的(p-1)次方除以p的余数恒等于1。注意9901为素数 。 两个整数,n和k(n ≤ 1000 , k ≤ 10 ^ 9)。
E N D
sicily 2501 算算式 _gXX遇到一个麻烦的式子: S = n1 + n2 + n3 + ...... + nk,已知n、k,求S的值。 因为_gXX数学很差,希望你能告诉他答案。但是由于他的数学实在太差了,所以你只需要告诉他S除以9901的余数即可。
费马小定理:假如p是质数,且(a,p)=1,那么 a^(p-1) ≡1(mod p),即假如p是质数,且a,p互质,那么 a的(p-1)次方除以p的余数恒等于1。注意9901为素数。 • 两个整数,n和k(n ≤ 1000 , k ≤ 10^9)。 (a*b) mod m = (a mod m)(b mod m) mod m; • 所以只需要计算数列的前9900项就可以了,后面就是前面的重复而已。
ural 1021 Sacrament of the Sum • Problem • Your program should decide, if it is possible to choose from two lists of integers such two numbers that their sum would be equal to 10 000. • :::each element of the lists lays in the range from -32768 to 32767
Sample Input Output • 4 YES • -175 • 19 • 19 • 10424 • 3 • 8951 • -424 • -788
MAP • Map是STL的一个关联容器,它提供一对一的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。 • map<datatype,datatype> name • 例如:map<string,int> a; • a["abc"] = 1;
map<int,int> a; • cin >> number; • a[10000 - number] = 1; • cin >> number1; • if(a[number1] == 1)
ural 1247Check a Sequence • There is a sequence of integer numbers A1, A2, …, AS, and a positive integer N. It's known that all elements of the sequence {Ai} satisfy the restriction 0 ≤ Ai ≤ 100. Moreover, it's known that the sum of all elements of the sequence is equal to S + N. You are to write a program that given a sequence {Ai} and a number N will answer the question: is it true that for all 1 ≤ i ≤ j ≤ S the following inequality holds: • Ai + Ai+1 + … + Aj ≤ (j – i + 1) + N ?
最大子段和问题:给出n个整数(可能为负数)A[1], A[2], ..., A[n],找到i和j(1<=i<=j<=n)使得A[i] + A[i+1] + ... + A[j]之和最大.
Algorithm • 一.暴力 • 二.分治法 • 三.DP
如果将所给的序列a[1:n]分为长度相等的两段a[1:n/2]和a[n/2+1:n],分别求出这两段的最大子段和,则a[1:n]的最大子段和有三种情况:如果将所给的序列a[1:n]分为长度相等的两段a[1:n/2]和a[n/2+1:n],分别求出这两段的最大子段和,则a[1:n]的最大子段和有三种情况: • (1) a[1:n]的最大子段和与a[1:n/2]的最大子段和相同 • (2) a[1:n]的最大子段和与a[n/2+1:n]的最大子段和相同 • (3) a[1:n]的最大子段和为a[i]+…+a[j],并且1<=i<=n/2,n/2+1<=j<=n。
对于(1)和(2)两种情况可递归求得,但是对于情况(3),容易看出a[n/2],a[n/2+1]在最大子段中。因此,我们可以在a[1:n/2]中计算出s1=max(a[n/2]+a[n/2-1]+…+a[i]),0<=i<=n/2,并在a[n/2+1:n]中计算出s2= max(a[n/2+1]+a[n/2+2]+…+a[i]),n/2+1<=i<=n。则s1+s2为出现情况(3)的最大子段和。
在对于上述分治算法的分析中我们注意到,若记b[j]=max(a[i]+a[i+1]+..+a[j]),其中1<=i<=j,并且1<=j<=n。则所求的最大子段和为max b[j],1<=j<=n。 • 由b[j]的定义可易知,当b[j-1]>0时b[j]=b[j-1]+a[j],否则b[j]=a[j]。故b[j]的动态规划递归式为: • b[j]=max(b[j-1]+a[j],a[j]),1<=j<=n。
01 #include<iostream> • 02 #include<memory.h> • 03 usingnamespacestd; • 04 intmain() • 05 { • 06 int n,k; • 07 inttb[10000]; • 08 while(cin>>n>>k) • 09 { • 10 intans=0; • 11 inttl=0; • 12 memset(tb,0,sizeof(tb)); • 13 tb[0]=1; • 14 for(inti=1;i<9901;i++) • 15 { • 16 tb[i]=n*tb[i-1]%9901; • 17 tl+=tb[i]; • 18 tl%=9901; • 19 } • 20 intcir=k/9900; • 21 inted=k%9900; • 22 ans=tl*cir%9901; • 23 for(inti=1;i<=ed;i++) • 24 ans=(ans+tb[i])%9901; • 25 cout<<ans<<endl; • 26 } • 27 }
01.#include<stdio.h> • 02.#define MAX 100 • 03.int maxsub(int left,int right); • 04.int a[MAX]; • 05.int main() • 06.{ • 07. int i; • 08. int count; • 09. scanf("%d",&count); • 10. for(i=0;i<count;i++) • 11. { • 12. scanf("%d",&a[i]); • 13. } • 14. printf("%d/n",maxsub(0,count-1)); • 15. return 0; • 16.} • 17.int maxsub(int left,int right) • 18.{ • 19. int center,i; • 20. int sum,left_sum,right_sum; • 21. int left_max,right_max; • 22. center=(left+right)/2; • 23. if(left==right) • 24. return a[left]; • 25. else • 26. { • 27. left_sum=maxsub(left,center); • 28. right_sum=maxsub(center+1,right); • 29. sum=0; • 30. left_max=0; • 31. for(i=center;i>=left;i--) • 32. { • 33. sum+=a[i]; • 34. if(sum>left_max) • 35. left_max=sum; • 36. } • 37. sum=0; • 38. right_max=0; • 39. for(i=center+1;i<=right;i++) • 40. { • 41. sum+=a[i]; • 42. if(sum>right_max) • 43. right_max=sum; • 44. } • 45. sum=right_max+left_max; • 46. if(sum<left_sum) • 47. sum=left_sum; • 48. if(sum<right_sum) • 49. sum=right_sum; • 50. } • 51. return sum; • 52.}
01.#include<stdio.h> • 02.int main() • 03.{ • 04. int count; • 05. int a[100]; • 06. int b[100]; • 07. int i; • 08. int max; • 09. scanf("%d",&count); • 10. for(i=0;i<count;i++) • 11. { • 12. scanf("%d",&a[i]); • 13. } • 14. b[0]=a[0]; • 15. max=b[0]; • 16. for(i=1;i<count;i++) • 17. { • 18. if(b[i-1]>0) • 19. b[i]=b[i-1]+a[i]; • 20. else • 21. b[i]=a[i]; • 22. if(b[i]>max) • 23. max=b[i]; • 24. } • 25. printf("%d/n",max); • 26. return 0; • 27.}