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 ...
随机推荐
- 划分树---hdu4417---区间查找(不)大于h的个数
http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others) ...
- [图算法] 1003. Emergency (25)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- Spring源码解析 – AnnotationConfigApplicationContext容器创建过程
Spring在BeanFactory基础上提供了一些列具体容器的实现,其中AnnotationConfigApplicationContext是一个用来管理注解bean的容器,从AnnotationC ...
- eclipse错误:Access restriction: The type 'BASE64Decoder' is not API
Access restriction: The type ‘BASE64Decoder’ is not API (restriction on required library ‘D:\java\jd ...
- mybatis(一)MyBatis Generator
在gradle中使用MyBatis Generator时,build.gradle配置如下: dependencies { mybatisGenerator group: 'org.mybatis.g ...
- appium1.6.3/1.6.4/1.6.5版本下如何支持安卓下ByName定位
1. 换其他定位方式,比如用xpath代替 2. 使用ByAccessibilityId代替,感觉没什么效果 一招修改源码解决问题根源,修改方法如下: 找到你的appium\node_modules\ ...
- xheditor在线编辑器在.netMVC4中的使用
在线编辑器xheditor,测试感觉不错,特把使用方法记录如下 : 先看看基本使用方法,然后用实例来操作 1.xheditor 地址 http://xheditor.com/ 2.下载最新编辑器源码 ...
- [三]SpringBoot 之 热部署
如下配置 <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring ...
- 洛谷 P2057 善意的投票(网络流最小割)
P2057 善意的投票 题目描述 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神.虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法 ...
- QT 主窗口和子窗口相互切换示例
QT 主窗口和子窗口相互切换示例 文件列表: SubWidget.h #ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QtWidgets/QW ...