Recently, the bear started studying data structures and faced the following problem.
You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: , where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).
Help the bear cope with the problem.
The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 107). The numbers are not necessarily distinct.
The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.
Print m integers — the answers to the queries on the order the queries appear in the input.
6 5 5 7 10 14 15 3 2 11 3 12 4 4
9 7 0
7 2 3 5 7 11 4 8 2 8 10 2 123
0 7
Consider the first sample. Overall, the first sample has 3 queries.
- The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9.
- The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7.
- The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.
题意:给你n个数(可能相等),然后给你q个查询,每个查询包含l r求l r中所有素数能整除n个数的个数的总和。
思路:筛法变形,在筛的过程中统计可以被整除的数的个数,然后求和,要求l r内的和等价于求sigema(0-r)-sigema(0-l)的和,输入进来用个数组计数就O了。
坑:如果l r很大记得把它直接设置为1e7否则会RE,然后之所以是筛法变形是因为筛的第二重循环不是j=2*i开始 而是j=i开始,因为一倍的情况也得累计。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=(int)(1e7+10);
bool is_prime[maxn];
int prime[maxn];
int cnt[maxn];
void dabiao()
{
memset(is_prime,0,sizeof(is_prime));
is_prime[0]=is_prime[1]=1;
for(int i=2;i<maxn;i++)
{
if(is_prime[i]!=0)
{
continue;
}
for(int j=i;j<maxn;j+=i)//尼玛得从1倍开始记录。。。
{
prime[i]+=cnt[j];//i的各种倍数也能整除i
is_prime[j]=1;
}
}
for(int i=2;i<maxn;i++)
{
prime[i]+=prime[i-1];//累加
}
}
int main()
{
int n;
cin>>n;
memset(prime,0,sizeof(prime));
memset(cnt,0,sizeof(cnt));
int a;
for(int i=0;i<n;i++)
{
cin>>a;
cnt[a]++;
}
dabiao();
int t;
cin>>t;
while(t--)
{
int l,r;
cin>>l>>r;
if(r>maxn)
r=maxn-1;
if(l>maxn)
l=maxn-1;
cout<<prime[r]-prime[l-1]<<endl;//l-r 等价于(0-r)-(0-l)
}
return 0;
}
Our bear's forest has a checkered field. The checkered field is an n × n table, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to right. Let's denote a cell of the field on the intersection of row x and column y by record (x, y). Each cell of the field contains growing raspberry, at that, the cell (x, y) of the field contains x + y raspberry bushes.
The bear came out to walk across the field. At the beginning of the walk his speed is (dx, dy). Then the bear spends exactly t seconds on the field. Each second the following takes place:
- Let's suppose that at the current moment the bear is in cell (x, y).
- First the bear eats the raspberry from all the bushes he has in the current cell. After the bear eats the raspberry from k bushes, he increases each component of his speed by k. In other words, if before eating the k bushes of raspberry his speed was (dx, dy), then after eating the berry his speed equals (dx + k, dy + k).
- Let's denote the current speed of the bear (dx, dy) (it was increased after the previous step). Then the bear moves from cell (x, y) to cell (((x + dx - 1) mod n) + 1, ((y + dy - 1) mod n) + 1).
- Then one additional raspberry bush grows in each cell of the field.
You task is to predict the bear's actions. Find the cell he ends up in if he starts from cell (sx, sy). Assume that each bush has infinitely much raspberry and the bear will never eat all of it.
The first line of the input contains six space-separated integers: n, sx, sy, dx, dy, t (1 ≤ n ≤ 109; 1 ≤ sx, sy ≤ n; - 100 ≤ dx, dy ≤ 100; 0 ≤ t ≤ 1018).
Print two integers — the coordinates of the cell the bear will end up in after t seconds.
思路:矩阵快速幂,6*6的一开始想成5*5的了,后来发现有问题因为 sx'=sx+dx+k=sx+dx+sx+sy-1=2sx-sy+dx-1,但是每次sx sy都在变化所以得再加一维记录。。
坑的地方:中途会出现负数= =不过看了HINT还好没坑到我。。 不过竟然写矩阵乘法不小心写错了一直过不了样例o(╯□╰)o。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
long long mod,sx,sy,dx,dy,t;
struct juzhen{
long long m[10][10];
};
juzhen mut(juzhen a,juzhen b)
{
juzhen c;
for(int i=1;i<=6;i++)
{
for(int j=1;j<=6;j++)
{
c.m[i][j]=0;
for(int k=1;k<=6;k++)
{
c.m[i][j]=((c.m[i][j]+a.m[i][k]*b.m[k][j])%mod+mod)%mod;
// c.m[i][j]%=mod;
}
}
}
return c;
}
juzhen power(juzhen a,long long p)
{
juzhen ans;
for(int i=1;i<=6;i++)
{
for(int j=1;j<=6;j++)
{
ans.m[i][j]=0;
}
}
for(int i=1;i<=6;i++)
{
ans.m[i][i]=1;
}
while(p)
{
if(p&1)
ans=mut(ans,a);
a=mut(a,a);
p>>=1;
}
return ans;
}
int main()
{
while(cin>>mod>>sx>>sy>>dx>>dy>>t)
{
juzhen ans;
juzhen base;
for(int i=1;i<=6;i++)
{
for(int j=1;j<=6;j++)
{
ans.m[i][j]=0;
}
}
ans.m[1][1]=ans.m[1][6]=ans.m[2][6]=ans.m[3][6]=ans.m[4][6]=ans.m[2][2]=2;
ans.m[1][2]=ans.m[1][3]=ans.m[1][5]=1;
ans.m[2][1]=ans.m[2][4]=ans.m[2][5]=1;
ans.m[3][1]=ans.m[3][2]=ans.m[3][3]=ans.m[3][5]=1;
ans.m[4][1]=ans.m[4][2]=ans.m[4][4]=ans.m[4][5]=1;
ans.m[5][5]=ans.m[5][6]=1;
ans.m[6][6]=1;
ans=power(ans,t);
base.m[1][1]=sx-1;
base.m[2][1]=sy-1;
base.m[3][1]=dx;
base.m[4][1]=dy;
base.m[5][1]=0;
base.m[6][1]=1;
base=mut(ans,base);
cout<<base.m[1][1]+1<<" "<<base.m[2][1]+1<<endl;
}
return 0;
}
那场比赛我也做了当时PRE了AB,结果最后全跪了(俗称爆零= =),当时简直弱的不能忍,D题看他的BLOG写的TAG是集合DP,不明觉厉就不看了。。。 昨天晚上写了C今天早上起来写了E赶脚还是不给力啊,没啥进步简直忧桑。。。