传送门

The Boss on Mars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2462    Accepted Submission(s): 760

Problem Description
On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger boss.



Due to no moons around Mars, the employees can only get the salaries per-year. There are n employees in ACM, and it’s time for them to get salaries from their boss. All employees are numbered from 1 to n. With the unknown reasons, if the employee’s work number
is k, he can get k^4 Mars dollars this year. So the employees working for the ACM are very rich.



Because the number of employees is so large that the boss of ACM must distribute too much money, he wants to fire the people whose work number is co-prime with n next year. Now the boss wants to know how much he will save after the dismissal.
 
Input
The first line contains an integer T indicating the number of test cases. (1 ≤ T ≤ 1000) Each test case, there is only one integer n, indicating the number of employees in ACM. (1 ≤ n ≤ 10^8)
 
Output
For each test case, output an integer indicating the money the boss can save. Because the answer is so large, please module the answer with 1,000,000,007.
 
Sample Input
  1. 2
  2. 4
  3. 5
 
Sample Output
  1. 82
  2. 354
  3. Hint
  4. Case1: sum=1+3*3*3*3=82
  5. Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
  6.  
 
Author
ZHANG, Chao
 
Source
 

题目大意:

给定 T 组数据。每组数据一个数 n ,然后让你求 <n且与 n 互素的数的四次方。详见例子。

解题思路:

就是就一个 S = a1^4 + a2^4 + ... + ak^4;

这个我们能够考虑容斥原理:也就是 1^4 + 2^4 + 3^4 + ... + n^4 - 不互素的数的四次方。

然后遇到了一个 非常严重的问题 就是 1^4 + 2^4 + 3^4 + ... + n^4怎么求;

首先 要推一下Sum =  1 + 2 + 3 + ... +n

(n+1)^2 - n^2 = 2*n+1;

n^2 - (n-1)^2 = 2*(n-1)+1;

...

3^2 - 2^2 = 2*2+1;

2^2 - 1^2 = 2*1+1;

将上述等式 左边加左边 右边加右边得到:

(n+1)^2 - 1 = 2*(1+2+3+...+n)+n*1;

又由于 我们所求 Sum =  1 + 2 + 3 + ... +n;

所以:2*Sum + n = (n+1)^2-1 =n^2+2*n+1-1 = n^2+2*n;

Sum = (n^2+n)/2。

所以 我们要求的四次方之和 就得用到 (n+1)^5 - n^5。(三次方的公式 首先得知道 就是通过刚才那个推出来的)

(x+1)^5 = x^5+5*x^4+10*x^3+10*x^2+5*x+1;

推完之后就是以下这个公式 还是挺费事儿的:

(1^4+2^4+……+n^4)=(n*(n+1)*(2n+1)*(3*n*n+3*n-1))/30;在这里 我们还要求一下 30 关于 MOD = 1e9+7的逆元,这个能够通过扩展欧几里得算法实现...

然后 就是 分析一下 代码的实现过程了:

第一步:我们首先进行素数筛 ;

第二步:我们就要进行素因子分解;

第三步:写一个高速幂 计算 X^4对 MOD取模

第四步:二进制枚举 。奇加偶减;

完事儿。

My Code:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. typedef long long LL;
  7. const LL MOD = 1e9+7;
  8. const LL MAXN = 1e6+5;
  9. LL Scan()///输入外挂
  10. {
  11. LL res=0,ch,flag=0;
  12. if((ch=getchar())=='-')
  13. flag=1;
  14. else if(ch>='0'&&ch<='9')
  15. res=ch-'0';
  16. while((ch=getchar())>='0'&&ch<='9')
  17. res=res*10+ch-'0';
  18. return flag?-res:res;
  19. }
  20.  
  21. inline void Out(LL a)///输出外挂
  22. {
  23. if(a>9)
  24. Out(a/10);
  25. putchar(a%10+'0');
  26. }
  27. LL n;
  28. /**
  29. void Ex_gcd(int a, int b, int &x, int &y)
  30. {
  31. if(b == 0)
  32. {
  33. x = 1;
  34. y = 0;
  35. return;
  36. }
  37. int x1, y1;
  38. Ex_gcd(b, a%b, x1, y1);
  39. x = y1;
  40. y = x1 - (a/b)*y1;
  41. }**/
  42. bool prime[MAXN];
  43. int p[MAXN];
  44. LL k;
  45. void isprime()
  46. {
  47. memset(prime, false, sizeof(prime));
  48. k = 0;
  49. for(LL i=2; i<MAXN; i++)
  50. {
  51. if(!prime[i])
  52. {
  53. p[k++] = i;
  54. for(LL j=i*i; j<MAXN; j+=i)
  55. prime[j] = true;
  56. }
  57. }
  58. }
  59. int fac[MAXN/100], num[MAXN/100];
  60. LL cnt;
  61. void Dec(LL m)
  62. {
  63. cnt = 0;
  64. memset(num, 0, sizeof(num));
  65. for(LL i=0; i<k&&p[i]*p[i]<=m; i++)
  66. {
  67. if(m%p[i] == 0)
  68. {
  69. fac[cnt] = p[i];
  70. while(m%p[i]==0)
  71. {
  72. m /= p[i];
  73. num[cnt]++;
  74. }
  75. cnt++;
  76. }
  77. }
  78. if(m > 1)
  79. {
  80. fac[cnt] = m;
  81. num[cnt++] = 1;
  82. }
  83. /**cout<<cnt<<endl;
  84. for(int i=0; i<cnt; i++)
  85. cout<<fac[i]<<" ";*/
  86. }
  87. LL Get_ans(LL x)
  88. {
  89. LL ans = x%MOD;
  90. ans = ans*(x+1)%MOD;
  91. ans = ans*(x+x+1)%MOD;
  92. ans = ans*((3*x*x+3*x-1)%MOD)%MOD;
  93. ans = ans*233333335%MOD;///233333335是逆元
  94. return ans;
  95. }
  96. LL quick_mod(LL a, LL b)
  97. {
  98. LL ans = 1;
  99. while(b)
  100. {
  101. if(b & 1)
  102. ans = ans*a%MOD;
  103. b>>=1;
  104. a = a*a%MOD;
  105. }
  106. return ans%MOD;
  107. }
  108.  
  109. int main()
  110. {
  111. isprime();
  112. ///Dec(6);
  113. ///int x, y;
  114. ///Ex_gcd(30,MOD, x, y)
  115. int T;
  116. T = Scan();
  117. while(T--)
  118. {
  119. n = Scan();
  120. Dec(n);
  121. LL res = 0;
  122. for(LL i=1; i<(1<<cnt); i++)
  123. {
  124. LL ans = 1, sum = 0;
  125. for(LL j=0; j<cnt; j++)
  126. {
  127. if(i & (1<<j))
  128. {
  129. sum++;
  130. ans = ans*fac[j]%MOD;
  131. }
  132. }
  133. if(sum&1)
  134. res = (res+Get_ans(n/ans)*quick_mod(ans,4)+MOD)%MOD;
  135. else
  136. res = (res-Get_ans(n/ans)*quick_mod(ans,4)+MOD)%MOD;
  137. }
  138. res = (Get_ans(n)-res+MOD)%MOD;
  139. res=(res%MOD+MOD)%MOD;
  140. printf("%lld\n",res);
  141. }
  142. return 0;
  143. }

HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)的更多相关文章

  1. HDU 4059 The Boss on Mars 容斥原理

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4059 The Boss on Mars(容斥原理)

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. hdu 4059 The Boss on Mars

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. 数论 + 容斥 - HDU 4059 The Boss on Mars

    The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...

  5. hdu 4059 The Boss on Mars(纳入和排除)

    http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4.如今减去与n互质的数的4次方.问共降低了多 ...

  6. hdu 4059 The Boss on Mars 容斥

    题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...

  7. hdu4059 The Boss on Mars 容斥原理

    On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger bo ...

  8. hdu4059The Boss on Mars 容斥原理

    //求1到n之间与n互质的数的四次方的和 //segma(n^4) = (6n^5+15n^4+10n^3-n)/30 //对于(a/b)%mod能够转化为(a*inv(b))%mod //inv(b ...

  9. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

随机推荐

  1. JSONPATH使用方法

    如下的json: { "store": { "book": [ { "category": "reference", & ...

  2. SourceTree的简单使用

    原文网址:http://blog.csdn.net/u011439289/article/details/42126507 今天开始参与公司项目的代码编写,公司内部采用的是gitlib,所以用到了So ...

  3. [Go] sync.Pool 的实现原理 和 适用场景

    摘录一: Go 1.3 的 sync 包中加入一个新特性:Pool. 官方文档可以看这里 http://golang.org/pkg/sync/#Pool 这个类设计的目的是用来保存和复用临时对象,以 ...

  4. mysqltool :dodba orzdba

    http://www.cnblogs.com/beyang/p/6963306.html

  5. delphi 文件查找

    FindFirst  是用来寻找目标目录下的第一个文件, FindFirst函数在delphi帮助下的定义: function FindFirst(const Path: string; Attr: ...

  6. JavaScript进阶系列06,事件委托

    在"JavaScript进阶系列05,事件的执行时机, 使用addEventListener为元素同时注册多个事件,事件参数"中已经有了一个跨浏览器的事件处理机制.现在需要使用这个 ...

  7. Android之 系统启动流程

    在前一篇文章"Android之 看“马达”如何贯通Android系统 (从硬件设计 --> 驱动 --> HAL --> JNI --> Framework --&g ...

  8. CoreTelephony.framework 引入程序方法

    #import <CoreTelephony/CTTelephonyNetworkInfo.h> #import <CoreTelephony/CTCarrier.h> 参考: ...

  9. [UI] 精美UI界面欣赏[13]

    精美UI界面欣赏

  10. 星际之门SG1第一至十季/全集Stargate SG-1迅雷下载

    英文译名 Stargate SG-1 (第一至十季) (1997-2008)Syfy.本季看点:<星际之门 SG-1>1997年起在美播出第一季,并于全球30多个国家播映,反应热烈,今年( ...