HDU 6211 卡常数取模 预处理 数论
求所有不超过1e9的 primitive Pythagorean triple中第2大的数取模$2^k$作为下标,对应a[i]数组的和。
先上WIKI:https://en.wikipedia.org/wiki/Pythagorean_triple
里面有通过欧几里得公式来得到有关毕达哥拉斯式子的一些性质。


最后得到的一个关于互质的m,n变种的式子更加直观,因此枚举m,n,保证其合法。每次枚举n,筛掉和n有共同因子的m,范围是$\sqrt{1e9}$。然后由于要求的是b,而且取模的都是2的幂指,因此可以先预处理所有$b%(2^{17})$的个数,最后再模$(2^k) $即可。
但是用这个方法做这道题还会卡取模的常数,需要把取模换成$\&(2^{17} - 1)$
另一提,半夜两点和偶像在做同这题,人家是1A秒杀,而且代码很短,还是蛮高兴的,并不(。
/** @Date : 2017-09-17 23:49:14
* @FileName: HDU 6211 青岛网络1006 欧几里得 数论.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 32000;
const double eps = 1e-8; int pri[N];
bool vis[N];
int c = 0;
int prime()
{
MMF(vis);
for(int i = 2; i < N; i++)
{
if(!vis[i]) pri[c++] = i;
for(int j = 0; j < c && i * pri[j] < N; j++)
{
vis[i * pri[j]] = 1;
if(i % pri[j] == 0) break;
}
}
}
LL coe[(1 << 17) + 20];
LL s[(1 << 17) + 20];
LL MA = (1LL<<17);
int main()
{
int T;
MMF(coe);
prime();
//cout << c << endl;
for(int i = 1; i * i <= 1000000000; i++)
{
MMF(vis);
int t = i;
for(int j = 0; j < c && pri[j] * pri[j] <= t; j++)
{
if(t % pri[j] == 0)
{
while(t % pri[j] == 0)
t /= pri[j];
for(int k = pri[j]; k <= N; k+=pri[j])
if(!vis[k]) vis[k] = 1;
}
}
if(t > 1)
for(int k = t; k < N; k+=t)
if(!vis[k]) vis[k] = 1;
for(int j = i + 1; j * j + i * i <= 1000000000; j++)
{
if(!vis[j]/*__gcd(i, j) == 1*/ && !(j&1 && i&1))
{
int a = 2 * i * j;
int b = j * j - i * i;
coe[max(a, b) & (MA - 1)]++;
//ans += s[max(a, b) % MA];
}
} //cout << ans << endl;
}
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
LL ans = 0;
int ma = (1 << n);
for(int i = 0; i < ma; i++)
scanf("%lld", s + i);
for(int i = 0; i < MA; i++)
ans += (LL)s[i & (ma - 1)] * coe[i];
printf("%lld\n", ans);
}
return 0;
}
/*
5
2
0 0 0 1
2
1 0 0 0
2
1 1 1 1
*/
//https://en.wikipedia.org/wiki/Pythagorean_triple
HDU 6211 卡常数取模 预处理 数论的更多相关文章
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- HDU 4632 区间DP 取模
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...
- HDU 5698 大组合数取模(逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu 4474 大整数取模+bfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...
- ACM卡常数(各种玄学优化)
首先声明,本博文部分内容仅仅适用于ACM竞赛,并不适用于NOIP与OI竞赛,违规使用可能会遭竞赛处理,请慎重使用!遭遇任何情况都与本人无关哈=7= 我也不想搞得那么严肃的,但真的有些函数在NOIP与O ...
- 【libreOJ模板】并查集(输入挂,取模与find优化)
1.了解了各种输入挂性orz,找到了一个合适的 2.find用while写能快一倍,并且能被数据卡掉 3.取模只能快十几毫秒,但也能被数据卡掉 取模find双优化是1997mm过的 再加一个性价比较高 ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
随机推荐
- AOP:spring 的Annotation配置
1.文件目录: 2.实体类 package com.wangcf.po; public class User { private int id; private String name; privat ...
- 查看dll依赖项
win7 系统: 开始-->所有程序->vs2012文件夹->vs tools->对应的命令提示符 输入命令: dumpbin /dependents 你的文件(可以是exe, ...
- Python:内建函数zip
1.语法 zip([iterable,...]) [说明]:iterable——一个或多个迭代器 2.功能 zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个的元组,然后返回由这 ...
- Servlet中常用对象及API类之间的关系
Servlet最常用的对象: 请求对象:ServletRequest和HttpServletRequest,通过该对象获取来自客户端的请求信息 响应对象:ServletResponse和HttpSer ...
- 78W的数据使用forall 进行批量转移;
create or replace procedure test_forall(CURRENTPAGE number ) as .--CURRENTPAGE number :=2 ; .PAGESIZ ...
- 爬虫学习之-urlparse之urljoin()
首先导入模块,用help查看相关文档 >>> from urlparse import urljoin >>> help(urljoin) Help on func ...
- 【Python】第一篇:python基础_1
本篇内容 Python介绍 安装 第一个程序(hello,world) 变量 用户输入(input) 数据类型 数据运算 if判断 break和continue的区别 while 循环 一. Pyth ...
- 【Java】判断字符串是否包含子字符串
JAVA里面判断: public static void main(String[] args) { String str="ABC_001"; if(str.indexOf(&q ...
- [六]SpringBoot 之 连接数据库(mybatis)
在进行配置之前首先要了解springboot是如何使用纯java代码方式初始化一个bean的 以前的版本是在xml中使用beans标签,在其里面配置bean,那么纯Java代码怎么实现呢? 答案就是使 ...
- 洛谷P1352 没有上司的舞会——树形DP
第一次自己写树形DP的题,发个博客纪念`- 题目来源:P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结 ...