【THUSC2017】【LOJ2978】杜老师 高斯消元
题目大意
给你 \(l,r\),求从 \(l\) 到 \(r\) 这 \(r-l+1\) 个数中能选出多少个不同的子集,满足子集中所有的数的乘积是一个完全平方数。
对 \(998244353\) 取模。
\(1\leq l,r\leq {10}^7\)
有 \(100\) 组数据,\(\sum r-l+1\leq 6\times {10}^7\)
题解
对于每个数,求出这个数中包含了哪些出现次数为奇数的质数。
那么就可以直接高斯消元,记矩阵的秩为 \(r\),答案就是 \(2^{r-l+1-r}\)。可以用 bitset 优化。
时间复杂度为 \(O(\frac{n\pi(n)^2}{w})\)。
可以发现,一个数最多有一个 \(>\sqrt r\) 的质因子。那么对于两个最大值因子相同的数,可以让第二个数的状态异或上第一个数的状态,这样第二个数的状态就只有 \(\leq \sqrt r\) 的质因子了。
这样就可以让矩阵的列的数量降低到 \(\pi(\sqrt n)\)。
但是还是过不了这题。
可以发现,当 \(r-l+1\) 足够大的时候就可以认为这个矩阵满秩了。在本题中,当 \(r-l+1>6000\) 的时候就可以不用高斯消元直接求出答案了。
时间复杂度:\(O(\frac{T\times 6000\times \pi(\sqrt n)^2}{w})\)。
这个复杂度很松,实际跑起来非常快。
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<cmath>
#include<vector>
#include<assert.h>
#include<bitset>
using namespace std;
using std::min;
using std::max;
using std::swap;
using std::sort;
using std::reverse;
using std::random_shuffle;
using std::lower_bound;
using std::upper_bound;
using std::unique;
using std::vector;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
void open(const char *s){
#ifndef ONLINE_JUDGE
char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
void open2(const char *s){
#ifdef DEBUG
char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
int rd(){int s=0,c,b=0;while(((c=getchar())<'0'||c>'9')&&c!='-');if(c=='-'){c=getchar();b=1;}do{s=s*10+c-'0';}while((c=getchar())>='0'&&c<='9');return b?-s:s;}
void put(int x){if(!x){putchar('0');return;}static int c[20];int t=0;while(x){c[++t]=x%10;x/=10;}while(t)putchar(c[t--]+'0');}
int upmin(int &a,int b){if(b<a){a=b;return 1;}return 0;}
int upmax(int &a,int b){if(b>a){a=b;return 1;}return 0;}
const ll p=998244353;
const int N=10000010;
const int n=10000000;
const int sqrtn=3162;
const int size=446;
typedef bitset<500> arr;
ll fp(ll a,ll b)
{
ll s=1;
for(;b;b>>=1,a=a*a%p)
if(b&1)
s=s*a%p;
return s;
}
int c[N],d[N],b[N],pri[N],cnt;
void sieve()
{
c[1]=1;
for(int i=2;i<=n;i++)
{
if(!b[i])
{
pri[++cnt]=i;
d[i]=cnt;
c[i]=i;
}
for(int j=1;j<=cnt&&i*pri[j]<=n;j++)
{
int v=i*pri[j];
b[v]=1;
c[v]=c[i];
if(i%pri[j]==0)
break;
}
}
}
void init()
{
sieve();
}
arr get(int x)
{
while(c[x]>sqrtn)
x/=c[x];
arr res;
while(x>1)
{
res.flip(d[c[x]]-1);
x/=c[x];
}
return res;
}
int len,tot,tot2;
void solve2(int l,int r)
{
tot=0;
len=r-l+1;
for(int i=1;i<=cnt;i++)
if(r/pri[i]!=(l-1)/pri[i])
tot++;
ll ans=fp(2,len-tot);
printf("%lld\n",ans);
}
arr e[size];
int insert(arr v)
{
for(int i=0;i<size;i++)
if(v[i])
{
if(e[i][i])
v^=e[i];
else
{
e[i]=v;
return 1;
}
}
return 0;
}
pii a[10000];
arr pre;
int cmp(pii a,pii b)
{
return a.second<b.second;
}
void solve()
{
int l,r;
scanf("%d%d",&l,&r);
if(r-l>6000)
{
solve2(l,r);
return;
}
tot=0;
tot2=0;
len=r-l+1;
if(l==1)
l++;
int m=0;
for(int i=0;i<size;i++)
e[i].reset();
for(int i=l;i<=r;i++)
a[++m]=pii(i,c[i]);
sort(a+1,a+m+1,cmp);
for(int i=1;i<=m;i++)
if(a[i].second<=sqrtn)
{
if(tot<size)
if(insert(get(a[i].first)))
tot++;
}
else if(i==1||a[i].second!=a[i-1].second)
{
tot2++;
if(tot<size)
pre=get(a[i].first);
}
else
{
if(tot<size)
if(insert(get(a[i].first)^pre))
tot++;
}
ll ans=fp(2,len-tot-tot2);
printf("%lld\n",ans);
}
int main()
{
open("dls");
init();
int t;
scanf("%d",&t);
while(t--)
solve();
return 0;
}
【THUSC2017】【LOJ2978】杜老师 高斯消元的更多相关文章
- 【THUSC2017】杜老师
题目描述 杜老师可是要打+∞年World Final的男人,虽然规则不允许,但是可以改啊! 但是今年WF跟THUSC的时间这么近,所以他造了一个idea就扔下不管了…… 给定L,R,求从L到R的这R− ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- 【BZOJ-3270】博物馆 高斯消元 + 概率期望
3270: 博物馆 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 292 Solved: 158[Submit][Status][Discuss] ...
- *POJ 1222 高斯消元
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9612 Accepted: 62 ...
- [bzoj1013][JSOI2008][球形空间产生器sphere] (高斯消元)
Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球 面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧 ...
- hihoCoder 1196 高斯消元·二
Description 一个黑白网格,点一次会改变这个以及与其连通的其他方格的颜色,求最少点击次数使得所有全部变成黑色. Sol 高斯消元解异或方程组. 先建立一个方程组. \(x_i\) 表示这个点 ...
- BZOJ 2844 albus就是要第一个出场 ——高斯消元 线性基
[题目分析] 高斯消元求线性基. 题目本身不难,但是两种维护线性基的方法引起了我的思考. void gauss(){ k=n; F(i,1,n){ F(j,i+1,n) if (a[j]>a[i ...
- SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元
[题目分析] Matrix-Tree定理+高斯消元 求矩阵行列式的值,就可以得到生成树的个数. 至于证明,可以去看Vflea King(炸树狂魔)的博客 [代码] #include <cmath ...
- UVALive 7138 The Matrix Revolutions(Matrix-Tree + 高斯消元)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
随机推荐
- java--基本数据类型的转换(自动转换)
概念:Java中,经常可以遇到类型转换的场景,从变量的定义到复制.数值变量的计算到方法的参数传递.基类与派生类间的造型等,随处可见类型转换的身影.Java中的类型转换在Java编码中具有重要的作用.首 ...
- git status 显示中文乱码
场景 在使用git命令行查看当前 状态时, git status 显示中文文件乱码: 解决 修改git配置, git config --global core.quotepath false
- Nginx设置Https反向代理,指向Docker Gitlab11.3.9 Https服务
目录 目录 1.GitLab11.3.9的安装 2.域名在阿里云托管,申请免费的1年证书 3.Gitlab 的 https 配置 4.Nginx 配置 https,反向代理指向 Gitlab 配置 目 ...
- MyDAL - 引用类型对象 .DeepClone() 深度克隆[深度复制] 工具 使用
索引: 目录索引 一.API 列表 .DeepClone() 用于 Model / Entity / ... ... 等引用类型对象的深度克隆 特性说明 1.不需要对对象做任何特殊处理,直接 .Dee ...
- JS中 confirm() 方法
前言 环境: window 10,google 浏览器 测试代码 <html> <!-- 测试确定框,如果点 "是" ,则返回 true,这样就触发 a 标签的 ...
- Powershell-远程操作
1. 查看WinRM是否开启 Get-Service WinRM 2. Enable-PSRemoting –Force 3. 进行信任设置: Set-Item wsman:\localhost\cl ...
- js用canvans 实现简单的粒子运动
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 在chrome 怎么通过ajax请求加载本地文件
在chrome下面用Jquery 的load方法加载本地的html文件时会报错 我百度了一下是因为 谷歌浏览器内核为了安全机制,不允许这样方式访问其他页面,但是可以通过加 --enable-file- ...
- WebApi接收post方式传入的json数据
[RoutePrefix("Api")] public class UploadController:BaseApiController { [HttpPost] [Route(& ...
- CRC32明文攻击
明文攻击是一种较为高效的攻击手段,大致原理是当你不知道一个zip的密码,但是你有zip中的一个已知文件(文件大小要大于12Byte)时, 因为同一个zip压缩包里的所有文件都是使用同一个加密密钥来加密 ...