Remoteland HDU - 4196
题意:
给出一个n,在[1, n] 中挑选几个不同的数相乘,求能的到的最大完全平方数
解析:
最大的肯定是n!, 然后n!不一定是完全平方数 (我们知道一个完全平方数,质因子分解后,所有质因子的质数均为偶数)
用勒让德定理求出每个质数在n!中的数量,如果是奇数,则除去一个这个数,偶数不操作
(输出用%I64d
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e7, INF = 0x7fffffff;
const LL MOD = 1e9 + ;
int prime[maxn + ];
int n;
int ans;
void get_prime()
{
ans = ;
for(int i = ; i <= maxn; i++)
{
if(!prime[i]) prime[++ans] = i;
for(int j = ; j <= ans && prime[j] <= maxn / i; j++)
{
prime[i * prime[j]] = ;
if(i % prime[j] == ) break;
} }
}
int f[maxn + ]; void init()
{
f[] = ;
for(int i = ; i <= maxn; i++)
f[i] = (LL)f[i - ] * i % MOD;
} LL check(LL x, LL p)
{
LL ret = ;
LL P = p;
while(P <= x)
{
ret += x / P;
P = p * P;
}
return ret;
} LL q_pow(LL a, LL b)
{
LL res = ;
while(b)
{
if(b & ) res = res * a % MOD;
a = a * a % MOD;
b >>= ;
}
return res;
} int main()
{
get_prime();
init();
while(scanf("%d", &n) != EOF && n)
{
LL res = , pri = ;
for(int i = ; i <= ans && prime[i] <= n; i++)
{
LL cnt = check(n, prime[i]);
// cout << cnt << " " << prime[i] << endl;
if(cnt & ) pri = (LL)pri * prime[i] % MOD;
}
// cout << pri << endl;
res = (LL)(f[n] * q_pow(pri, MOD - ) % MOD); printf("%I64d\n", res); } return ;
}
Remoteland HDU - 4196的更多相关文章
- HDU 4196 Remoteland
题意:给定一个n,然后让你从1-n中选出某些数乘起来,使得乘积最大,并且乘积必须是完全平方数. 思路:将1-n种每个数都分解素因子,把他们的素因子的幂加起来,如果是偶数,就说明可以构成完全平方数,乘起 ...
- HDU 4196
很容易由算术基本定理知道,完全平方数就是所有质因子指数为偶数的数.而求得N以下的质因子,可由前两篇的公式知,由N!与p的关系求得.对于指数为p的,用N!除去就可以,因为p必定属于N以内,且无重复. 至 ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- leetcode67
public class Solution { public string AddBinary(string a, string b) { var list = new List<string& ...
- div高度自适应的问题
对象height:100%并不能直接产生效果,是因为跟其父对象有关. #center{height:100%;} 上面的css样式是无效的,不会产生任何效果. 需要改写: html,body{ m ...
- 使用 Zend_Studio 开发
之前一直用vim 写PHP, 总觉得IDE的一大堆没有用的插件非常麻烦,所以一直避免使用Zend_Studio.不过随着PHP的发展和框架的发展,以及个人的发展,最后还是回到的IDE的时代. 在使用Z ...
- centos7 安装 openvswitch
1.安装依赖包: yum -y install make gcc openssl-devel autoconf automake rpm-build redhat-rpm-config yum - ...
- 若p是与10互质的质数,则p-1个9能被p整除
[若p是与10互质的质数,则k(p-1)个9能被p整除] 因为(p,10)=1,所以(p,10^k)=1.根据费马定理,10^(k*(p-1))-1|p. 而10^k*(p-1)-1是一个位数为(p- ...
- go_goroutine and channel
协程Coroutine 轻量级”线程“ 非抢占式多任务处理,由协程主动交出控制权(如果程序中间没有print,程序会一直霸占着调度器) 编译器/解释器/虚拟机层面的多任务 多个协程可能在一个或多个线程 ...
- Python any() 函数
Python any() 函数 Python 内置函数 描述 any() 函数用于判断给定的可迭代参数 iterable 是否全部为 False,则返回 False,如果有一个为 True,则返回 ...
- java高级工程师(一)
一.无笔试题 不知道是不是职位原因还是没遇到,面试时,都不需要做笔试题,而是填张个人信息表格,或者直接面试 二.三大框架方面问题 1.Spring 事务的隔离性,并说说每个隔离性的区别 ...
- SQLSERVER CROSS APPLY 与 OUTER APPLY 的应用
日常开发中遇到多表查询时,首先会想到 INNER JOIN 或 LEFT OUTER JOIN 等等,但是这两种查询有时候不能满足需求.比如,左表一条关联右表多条记录时,我需要控制右表的某一条或多条记 ...
- c++ template 判断是否为类类型
/* The following code example is taken from the book * "C++ Templates - The Complete Guide" ...