hdu 4059 The Boss on Mars
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
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.
2
4
5
82
354HintCase1: sum=1+3*3*3*3=82
Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
题解及代码:
这道题目的综合性还是非常强的。首先说一下题目,就是求小于n而且与n互素的数的四次方的和。
说一下思路吧:首先我们求出1---n-1的全部的数的四次方的和,之后将n进行素因子分解。求出n的全部因子,然后减去包括这些因子的数的四次方就能够了。
大体上的思路有了,来处理一下细节:1.首先我们要求出四次方和的公式 2.素数打表 3.求逆元(由于四次方和公式有一个分母,取余时要乘上逆元)
4.素因子分解 5.容斥原理
搞定这5步,我们这道题就能做了,所以说综合性很强。
详细见代码吧:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const long long mod=1000000007,q=233333335;//p为逆元,用费马小定理求出
bool prime[10010];
int p[1400];
int k=0; //四次方和计算公式
long long cal(long long n)
{
if(n==0) return 0;
return (n*(n+1)%mod*(2*n+1)%mod)%mod*((3*n*n+3*n-1)%mod*q%mod)%mod;
} //容斥原理
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[])
{
if(nt==m)
{
long long b=n/mu;
if(m%2==0)
{
sum=(sum-mu*mu%mod*mu%mod*mu%mod*cal(b)%mod+mod)%mod;
}
else
{
sum=(sum+mu*mu%mod*mu%mod*mu%mod*cal(b)%mod)%mod;
}
return;
}
for(long long i=base; i<num_p; i++)
{
dfs(i+1,num_p,n,m,nt+1,mu*tab_p[i],sum,tab_p);
}
} //素数打表
void isprime()
{
long long i,j;
memset(prime,true,sizeof(prime));
prime[0]=prime[1]=false;
for(i=2; i<10010; i++)
{
if(prime[i])
{
p[k++]=i;
for(j=i*i; j<10010; j+=i)
prime[j]=false;
}
}
} int main()
{
isprime();
long long n,ans,tab_p[1400];
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%I64d",&n);
n=n-1;
ans=cal(n);
long long m=n,t=n+1;
int num_p=0;
for(int i=0; i<k&&p[i]*p[i]<=t; i++) //素因子分解
if(t%p[i]==0)
{
tab_p[num_p++]=p[i];
while(t%p[i]==0)
{
t/=p[i];
}
}
if(t>1) tab_p[num_p++]=t; /*//输出測试
for(int i=0;i<num_p;i++)
{
printf("%d ",tab_p[i]);
}
puts("");
//測试结束
*/ long long sum=0;
for(int i=0; i<num_p; i++) //将不互素的部分减去
{
n=m/tab_p[i];
sum=(sum+tab_p[i]*tab_p[i]%mod*tab_p[i]%mod*tab_p[i]%mod*cal(n))%mod;
} for(long long i=2; i<=num_p; i++) //容斥部分求解
dfs(0,num_p,m,i,0LL,1LL,sum,tab_p); printf("%I64d\n",(ans-sum+mod)%mod);
}
return 0;
}
hdu 4059 The Boss on Mars的更多相关文章
- HDU 4059 The Boss on Mars 容斥原理
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)
传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 数论 + 容斥 - HDU 4059 The Boss on Mars
The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...
- HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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次方.问共降低了多 ...
- hdu 4059 The Boss on Mars 容斥
题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
- HDU 4059 容斥原理+快速幂+逆元
E - The Boss on Mars Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- 1.JPA概要
转自:https://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html JPA定义了Java ORM及实体操作API的标准.本文摘录了J ...
- Linux 解压缩命令整理
一.tar命令 参数 参数 详解 参数 详解 -c 可以使用绝对路径来压缩 -x 解开一个压缩文件的参数指令 -t 查看内容 -r 向压缩归档文件末尾追加文件 -u 更新原压缩包中的文件 -z 有gz ...
- Spring MVC handler interceptors example--转载
原文地址:http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/ Spring MVC allow you ...
- DIV+CSS学习笔记
第十五章 定位 static静态定位(不对它的位置进行改变,在哪里就在那里) 默认值.没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明 ...
- 【例题 6-15 UVA - 10129】Play on Words
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 拓扑大水题 [代码] #include <bits/stdc++.h> using namespace std; con ...
- netty reactor线程模型分析
netty4线程模型 ServerBootstrap http示例 // Configure the server. EventLoopGroup bossGroup = new EpollEvent ...
- PHP URL参数获取方式的四种例子
在已知URL参数的情况下,我们可以根据自身情况采用$_GET来获取相应的参数信息($_GET['name']);那,在未知情况下如何获取到URL上的参数信息呢? 第一种.利用$_SERVER内置数组变 ...
- 转 OC温故:类的三大特性(封装,继承,多态)
原文标题:OC学习篇之---类的三大特性(封装,继承,多态) 我们都知道,面向对象程序设计中的类有三大特性:继承,封装,多态,这个也是介绍类的时候,必须提到的话题,那么今天就来看一下OC中类的三大特性 ...
- html中radio、checkbox选中状态研究(静下心来看,静下心来总结)
html中radio.checkbox选中状态研究(静下心来看,静下心来总结) 一.总结 1.单选框的如果有多个checked 会以最后一个为准 2.js动态添加checked属性:不行:通过 $(& ...
- AE中地图查询方式
樱木 原文 AE中地图查询方式 地图查询主要有两种查询:空间查询和属性查询 所用到知识点: 1 Cursor(游标)对象 本质上是一个指向数据的指针,本身不包含数据内容,提供一个连接到ROW对象或者 ...