[关闭]
@geek-sjl 2018-09-25T15:27:17.000000Z 字数 1113 阅读 449

18图灵班编程作业一

Task1

Code

  1. #include <cstdio>
  2. int count(int n){
  3. int sum=0;
  4. for(int i=1;i<=n-1;i++){
  5. sum+=i;
  6. }
  7. printf("%d\n",sum%n);
  8. }
  9. int main(){
  10. for(int i=1;i<=10000;i++){
  11. count(i);
  12. }
  13. return 0;
  14. }

Conclusion

Proof

No matter n is odd or even ,we could get that


We could make f(n)=kn,whether k is integer depends on

When n is odd,n-1 is even,then k is integer,so the result is zero.
When n is even,n-2 is even,and


Since n-2 is even,f(n) mod n equal

Task2

Code

  1. #include <cstdio>
  2. #include <cmath>
  3. long long count(int i,int n){
  4. long long sum=0;
  5. for(int t=1;t<=n-1;t++){
  6. sum+=pow(t,i);
  7. sum%=n;
  8. }
  9. return sum;
  10. }
  11. int main(){
  12. for(int n=2;n<=15;n++){
  13. for(int i=2;i<=n-1;i++){
  14. printf("%4d ",count(i,n));
  15. }
  16. printf("\n");
  17. }
  18. return 0;
  19. }

Conclusion

Here is the output
Because the result of pow() is too large,n couldn't over 15.

   2
   2    0
   0    0    4
   1    3    1    3
   0    0    0    0    6
   4    0    4    0    4    0
   6    0    6    0    6    0    6
   5    5    3    5    5    5    3    5
   0    0    0    0    0    0    0    0   10
   2    0    2    0    2    0    2    0    2    0
   0    0    0    0    0    0    0    0    0    0   12
   7    7    7    7    5    7    7    7    7    7    5    7
  10    0    7    0   10    0    7    0   10    0    7    0    9

It is not finished,emm....give me more time...,

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注