【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 ...
随机推荐
- linux-2.6.18源码分析笔记---中断
一.中断初始化 中断的一些硬件机制不做过多的描述,只介绍一些和linux实现比较贴近的机制,便于理解代码. 1.1 关于intel和linux几种门的简介 intel提供了4种门:系统门,中断门,陷阱 ...
- 杭电ACM2020--绝对值排序
输入n(n<=100)个整数,按照绝对值从大到小排序后输出.题目保证对于每一个测试实例,所有的数的绝对值都不相等. Input 输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整 ...
- C#根据屏幕分辨率改变图片尺寸
最近工作中遇到一个问题,就是需要将程序文件夹中的图片根据此时电脑屏幕的分辨率来重新改变图片尺寸 以下为代码实现过程: 1.获取文件夹中的图片,此文件夹名为exe程序同目录下 //读取文件夹中文件 Di ...
- java爬虫系列第三讲-获取页面中绝对路径的各种方法
在使用webmgiac的过程中,很多时候我们需要抓取连接的绝对路径,总结了几种方法,示例代码放在最后. 以和讯网的一个页面为例: xpath方式获取 log.info("{}", ...
- 多态以及 LeetCode 每日一题
1 多态 1.1 多态性 Java 引用变量有两个类型:一个是编译时类型,一个是运行时类型.前者是代码中声明这个变量时的类型,后者是由实际对象的类型决定的.当编译类型和运行类型不一样时,产生多态. c ...
- OO_BLOG2_多线程电梯模拟
作业2-1 单部多线程傻瓜调度(FAFS)电梯的模拟 I. 基于度量的程序结构分析 1)程序结构与基本度量统计图 2)分析 这次作业基本奠定了本人三次电梯作业的基本架构,简述如下: Elevato ...
- int 与 Integer 的区别
int和Integer的区别 Integer是int的包装类,int则是java的一种基本数据类型 Integer变量必须实例化后才能使用,而int变量不需要 Integer实际是对象的引用,当new ...
- java 线程方法 ---- wait()
class MyThread5 implements Runnable{ private int flag = 10; @Override public void run() { while (fla ...
- 【设计模式】工厂方法模式 Factory Method Pattern
在简单工厂模式中产品的创建统一在工厂类的静态工厂方法中创建,体现了面形对象的封装性,客户程序不需要知道产品产生的细节,也体现了面向对象的单一职责原则(SRP),这样在产品很少的情况下使用起来还是很方便 ...
- 华为6.0系统设备最完美激活Xposed框架的经验
对于喜欢玩手机的伙伴而言,经常会使用上Xposed框架及种种功能强大的模块,对于5.0以下的系统版本,只要手机能获得root权限,安装和激活Xposed框架是非常轻松的,但随着系统版本的迭代,5.0以 ...