The Boss on Mars

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

Total Submission(s): 1934    Accepted Submission(s): 580

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
 

题解及代码:

这道题目的综合性还是非常强的。首先说一下题目,就是求小于n而且与n互素的数的四次方的和。

说一下思路吧:首先我们求出1---n-1的全部的数的四次方的和,之后将n进行素因子分解。求出n的全部因子,然后减去包括这些因子的数的四次方就能够了。

大体上的思路有了,来处理一下细节:1.首先我们要求出四次方和的公式   2.素数打表   3.求逆元(由于四次方和公式有一个分母,取余时要乘上逆元)

4.素因子分解    5.容斥原理

搞定这5步,我们这道题就能做了,所以说综合性很强。

详细见代码吧:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. const long long mod=1000000007,q=233333335;//p为逆元,用费马小定理求出
  8. bool prime[10010];
  9. int p[1400];
  10. int k=0;
  11.  
  12. //四次方和计算公式
  13. long long cal(long long n)
  14. {
  15. if(n==0) return 0;
  16. return (n*(n+1)%mod*(2*n+1)%mod)%mod*((3*n*n+3*n-1)%mod*q%mod)%mod;
  17. }
  18.  
  19. //容斥原理
  20. void dfs(int base,int num_p,long long n,long long m,long long nt,long long mu,long long &sum,long long tab_p[])
  21. {
  22. if(nt==m)
  23. {
  24. long long b=n/mu;
  25. if(m%2==0)
  26. {
  27. sum=(sum-mu*mu%mod*mu%mod*mu%mod*cal(b)%mod+mod)%mod;
  28. }
  29. else
  30. {
  31. sum=(sum+mu*mu%mod*mu%mod*mu%mod*cal(b)%mod)%mod;
  32. }
  33. return;
  34. }
  35. for(long long i=base; i<num_p; i++)
  36. {
  37. dfs(i+1,num_p,n,m,nt+1,mu*tab_p[i],sum,tab_p);
  38. }
  39. }
  40.  
  41. //素数打表
  42. void isprime()
  43. {
  44. long long i,j;
  45. memset(prime,true,sizeof(prime));
  46. prime[0]=prime[1]=false;
  47. for(i=2; i<10010; i++)
  48. {
  49. if(prime[i])
  50. {
  51. p[k++]=i;
  52. for(j=i*i; j<10010; j+=i)
  53. prime[j]=false;
  54. }
  55. }
  56. }
  57.  
  58. int main()
  59. {
  60. isprime();
  61. long long n,ans,tab_p[1400];
  62. int cas;
  63. scanf("%d",&cas);
  64. while(cas--)
  65. {
  66. scanf("%I64d",&n);
  67. n=n-1;
  68. ans=cal(n);
  69. long long m=n,t=n+1;
  70. int num_p=0;
  71. for(int i=0; i<k&&p[i]*p[i]<=t; i++) //素因子分解
  72. if(t%p[i]==0)
  73. {
  74. tab_p[num_p++]=p[i];
  75. while(t%p[i]==0)
  76. {
  77. t/=p[i];
  78. }
  79. }
  80. if(t>1) tab_p[num_p++]=t;
  81.  
  82. /*//输出測试
  83. for(int i=0;i<num_p;i++)
  84. {
  85. printf("%d ",tab_p[i]);
  86. }
  87. puts("");
  88. //測试结束
  89. */
  90.  
  91. long long sum=0;
  92. for(int i=0; i<num_p; i++) //将不互素的部分减去
  93. {
  94. n=m/tab_p[i];
  95. sum=(sum+tab_p[i]*tab_p[i]%mod*tab_p[i]%mod*tab_p[i]%mod*cal(n))%mod;
  96. }
  97.  
  98. for(long long i=2; i<=num_p; i++) //容斥部分求解
  99. dfs(0,num_p,m,i,0LL,1LL,sum,tab_p);
  100.  
  101. printf("%I64d\n",(ans-sum+mod)%mod);
  102. }
  103. return 0;
  104. }

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/Oth ...

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

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

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

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

  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(差分+容斥原理)

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

  8. HDU 4059 容斥原理+快速幂+逆元

    E - The Boss on Mars Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  9. The Boss on Mars

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

随机推荐

  1. mysql-cacti-templates-1.1.2.tar.gz 免费下载 cacti MySQL添加监控

    cacti MySQL添加监控 1. 安装监控插件 wget http://mysql-cacti-templates.googlecode.com/files/mysql-cacti-templat ...

  2. 1.1selenium 介绍

    1.1selenium 介绍selenium 是一个 web 的自动化测试工具,不少学习功能自动化的同学开始首选 selenium , 相因为它相比 QTP 有诸多有点:* 免费,也不用再为破解 QT ...

  3. 不在JPA 的 persistence.xml 文件中配置Entity class的解决办法

    在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,2种方法可以解决此问题: 这2种方式都可以实现不用在persiste ...

  4. Ubuntu 14.04安装teamviewer 远程桌面

    teamviewer 真是一款非常强大的远程登录软件,可以跨Windows和Ubuntu远程登录,但是在64bit的Ubuntu下安装时,按照官方安装方法总是会遇到问题,下面说一下如何安装: 安装i3 ...

  5. 【Codeforces Round #445 (Div. 2) B】Vlad and Cafes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 傻逼模拟 [代码] #include <bits/stdc++.h> using namespace std; cons ...

  6. C#利用反射机制,获取实例的属性和属性值

    C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 对应某个类的实例化的对象tc, 遍历获取所有属性(子成员)的方法(采用反射): Type t = tc.GetType();// ...

  7. UVA 11374 Airport Express SPFA||dijkstra

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  8. js进阶 12-7 pageY和screenY以及clientY的区别是什么

    js进阶 12-7 pageY和screenY以及clientY的区别是什么 一.总结 一句话总结:pageY是距文件,screenY是获取显示器屏幕位置的坐标,clientY是页面视口. 没有滚动条 ...

  9. jQuery weui Select组件显示指定值

    jQuery weui有个支持单选或者多选的select弹出层,默认他是这样的 第2部分选择什么值,第1部分就显示什么值,一般的场景支持是没问题了,但本次开发碰到了一个问题. 需求描述: 职业名称后面 ...

  10. 怎么不让控制台system.out.println()打印

    1.System类有一个public static void setOut(PrintStream out)方法,你可以调用这个方法将out重定向到任何一个全局PrintStream对象上: 2.如果 ...