A. Nastya and an Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Nastya owns too many arrays now, so she wants to delete the least important of them. However, she discovered that this array is magic! Nastya now knows that the array has the following properties:

  • In one second we can add an arbitrary (possibly negative) integer to all elements of the array that are not equal to zero.
  • When all elements of the array become equal to zero, the array explodes.

Nastya is always busy, so she wants to explode the array as fast as possible. Compute the minimum time in which the array can be exploded.

Input

The first line contains a single integer 1≤n≤105) — the size of the array.

The second line contains −105≤ai≤105) — the elements of the array.


 

判除0外不同数字的个数。

 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clr_1(x) memset(x,-1,sizeof(x))
 4 #define INF 0x3f3f3f3f
 5 #define LL long long
 6 #define pb push_back
 7 #define mod 1000000007
 8 #define ls(i) (i<<1)
 9 #define rs(i) (i<<1|1)
10 #define mp make_pair
11 #define fi first
12 #define se second
13 using namespace std;
14 const int N=1e5+10;
15 set<LL> num;
16 int a[N];
17 int n;
18 int main()
19 {
20     cin>>n;
21     for(int i=1;i<=n;i++){
22         cin>>a[i];
23         if(a[i]==0) continue;
24         num.insert(a[i]);
25     }
26     printf("%d\n",num.size());
27     return 0;
28 }
View Code

 

B. Nastya Studies Informatics
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.

We define a pair of integers b.

You are given two integers a≠b.

Input

The only line contains four integers 1≤x≤y≤109).

Output

In the only line print the only integer — the answer for the problem.

Examples
input
Copy
1 2 1 2
output
Copy
2
input
Copy
1 12 1 12
output
Copy
4
input
Copy
50 100 3 30
output
Copy
0
Note

In the first example there are two suitable good pairs of integers (2,1).

In the second example there are four suitable good pairs of integers (4,3).

In the third example there are good pairs of integers, for example, l≤a,b≤r.

 


 

 

在sqrt(y) 范围内把所有可能的因数暴力找出来,然后暴力跑一遍这所有因数p和lcm/p看他们符不符合 $ p \ge l ,y/p \ge l $ 以及 $ p \le r , y/p \le r $ 并且 $ gcd(p,ly/p)==x $ 就行。符合就ans++。

 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clr_1(x) memset(x,-1,sizeof(x))
 4 #define INF 0x3f3f3f3f
 5 #define LL long long
 6 #define pb push_back
 7 #define mod 1000000007
 8 #define ls(i) (i<<1)
 9 #define rs(i) (i<<1|1)
10 #define mp make_pair
11 #define fi first
12 #define se second
13 using namespace std;
14 const int N=4e4+10;
15 const int P=4e4;
16 typedef pair<LL,LL> pll;
17 int inf[N],pri[N];
18 int tot;
19 void init()
20 {
21     tot=0;
22     for(int i=2;i<=P;i++)
23     {
24         if(!inf[i])
25             pri[++tot]=i;
26         for(int j=1;j<=tot && pri[j]*i<=P;j++)
27         {
28             inf[i*pri[j]]=1;
29             if(i%pri[j]==0) break;
30         }
31     }
32     return ;
33 }
34 set<LL> dive;
35 set<pll> ans;
36 int l,r,lcm,gcd;
37 bool infer(LL t)
38 {
39     return t>=l && t<=r;
40 }
41 int num[N];
42 int main()
43 {
44     scanf("%d%d%d%d",&l,&r,&gcd,&lcm);
45     for(LL d=1;d*d<=lcm;d++)
46         if(lcm%d==0)
47             dive.insert(d),dive.insert(lcm/d);
48     for(auto p:dive)
49     {
50         LL xx=gcd*p,yy=lcm/p;
51         if(infer(xx) && infer(yy) && __gcd(xx,yy)==gcd) ans.insert(mp(xx,yy));
52     }
53     printf("%d\n",ans.size());
54     return 0;
55 }
View Code

 

C. Nastya and a Wardrobe
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Nastya received a gift on New Year — a magic wardrobe. It is magic because in the end of each month the number of dresses in it doubles (i.e. the number of dresses becomes twice as large as it is in the beginning of the month).

Unfortunately, right after the doubling the wardrobe eats one of the dresses (if any) with the 50% probability. It happens every month except the last one in the year.

Nastya owns k+1 months.

Nastya is really busy, so she wants you to solve this problem. You are the programmer, after all. Also, you should find the answer modulo 109+7, because it is easy to see that it is always integer.

Input

The only line contains two integers k+1 is the number of months in a year in Byteland.

Output

In the only line print a single integer — the expected number of dresses Nastya will own one year later modulo 109+7.

Examples
input
Copy
2 0
output
Copy
4
input
Copy
2 1
output
Copy
7
input
Copy
3 2
output
Copy
21
Note

In the first example a year consists on only one month, so the wardrobe does not eat dresses at all.

In the second example after the first month there are (6+8)/2=7.

 


 

把期望计算式列出来可以发现这个答案就是一个式子。注意一下n的大小处理。

 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clr_1(x) memset(x,-1,sizeof(x))
 4 #define INF 0x3f3f3f3f
 5 #define LL long long
 6 #define pb push_back
 7 #define mod 1000000007
 8 #define ls(i) (i<<1)
 9 #define rs(i) (i<<1|1)
10 #define mp make_pair
11 #define fi first
12 #define se second
13 using namespace std;
14 LL n,k;
15 LL quickpow(LL x,LL n)
16 {
17     LL ans=1;
18     x%=mod;
19     while(n)
20     {
21         if(n&1)
22             ans=ans*x%mod;
23         x=x*x%mod;
24         n>>=1;
25     }
26     return ans%mod;
27 }
28 int main()
29 {
30     cin>>n>>k;
31     if(n==0)
32         printf("0\n");
33     else
34     {
35         n%=mod;
36         LL ans=((quickpow(2,k+1)*n%mod-(quickpow(2,k)-1+mod)%mod)%mod+mod)%mod;
37         ans%=mod;
38         printf("%I64d\n",ans);
39     }
40     return 0;
41 }
View Code

 

D. Nastya and a Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that k is a given constant for all subsegments.

Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array.

Input

The first line contains two integers k is the constant described above.

The second line contains 1≤ai≤108) — the elements of the array.

Output

In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k.

Examples
input
Copy
1 1
1
output
Copy
1
input
Copy
4 2
6 3 8 1
output
Copy
2
Note

In the first example the only subsegment is 11=1.

There are two situable subsegments in the second example — 2412=2.


 

我们看到数据范围,可以发现k*sum<= 2e18, 因此乘积不可能大于2e18,一旦大于往后就没有匹配线段了。我们枚举所有线段左端点,然后往后暴力跳右端点。我们每次跳的位置是一个非1的位置,这样最多跳60次。每次跳的范围内最多产生1个答案贡献。

 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clr_1(x) memset(x,-1,sizeof(x))
 4 #define INF 0x3f3f3f3f
 5 #define LL long long
 6 #define pb push_back
 7 #define ls(i) (i<<1)
 8 #define rs(i) (i<<1|1)
 9 #define mp make_pair
10 #define fi first
11 #define se second
12 using namespace std;
13 const int N=2e5+10;
14 const LL eps=1e18;
15 typedef pair<LL,LL> pll;
16 LL a[N];
17 LL aft[N];
18 LL ans;
19 int main()
20 {
21     int n;
22     LL k;
23     scanf("%d",&n);
24     scanf("%I64d",&k);
25     ans=0;
26     for(int i=1;i<=n;i++)
27         scanf("%I64d",a+i);
28     aft[n+1]=n+1;
29     aft[n]=n;
30     for(int i=n-1;i>=1;i--)
31         if(a[i]>1) aft[i]=i;
32         else aft[i]=aft[i+1];
33     LL sum,mul;
34     int pre,suf;
35     for(int i=1;i<=n;i++)
36     {
37         sum=0;
38         mul=1;
39         suf=i;
40         pre=i-1;
41         while(suf<=n)
42         {
43             sum+=a[suf];
44             if(eps/a[suf]<mul)
45                 break;
46             mul*=a[suf];
47             pre=suf;
48             suf=aft[suf+1];
49             if(mul%k==0 && mul/k>=sum && mul/k<=sum+suf-pre-1)
50                 ans++;
51             sum+=suf-pre-1;
52         }
53     }
54     printf("%I64d\n",ans);
55     return 0;
56 }
View Code

 

 

E. Nastya and King-Shamans
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Nastya likes reading and even spends whole days in a library sometimes. Today she found a chronicle of Byteland in the library, and it stated that there lived shamans long time ago. It is known that at every moment there was exactly one shaman in Byteland, and there were n in the order they lived. Also, each shaman had a magic power which can now be expressed as an integer.

The chronicle includes a list of powers of the n shamans. Also, some shamans can be king-shamans, if they gathered all the power of their predecessors, i.e. their power is exactly the sum of powers of all previous shamans. Nastya is interested in whether there was at least one king-shaman in Byteland.

Unfortunately many of the powers are unreadable in the list, so Nastya is doing the following:

  • Initially she supposes some power for each shaman.
  • After that she changes the power of some shaman q times (the shamans can differ) and after that wants to check if there is at least one king-shaman in the list. If yes, she wants to know the index of any king-shaman.

Unfortunately the list is too large and Nastya wants you to help her.

Input

The first line contains two integers 1≤n,q≤2⋅105).

The second line contains i-th shaman.

After that xi.

Output

Print i-th change.

If there are multiple king-shamans after each change, print the index of any of them.

Examples
input
Copy
2 1
1 3
1 2
output
Copy
-1
input
Copy
3 4
2 2 3
1 1
1 2
2 4
3 6
output
Copy
3
2
-1
3
input
Copy
10 7
0 3 1 4 6 2 7 8 10 1
2 5
1 3
9 36
4 10
4 9
1 2
1 0
output
Copy
1
-1
9
-1
4
-1
1
Note

In the first example powers of shamans after the first change are equal to 2.

In the second example after the first change the powers are equal to 3.


 

我们用bit存前缀和。然后我们在询问修改后,从tmp=a[1]开始,每次寻找一个位置t,它的前缀和大小pre不超过tmp*2-1,然后看t+1位置符不符合a[t+1] == pre[t] ,不符合则修改tmp为pre[t+1]继续倍增。由于我们要寻找的位置是a[p] == pre[p-1] 的这个p位置,因此这个倍增能很快速地在log2(n)*log2(n)的复杂度找到这个p位置。

 

 1 #include<bits/stdc++.h>
 2 #define clr(x) memset(x,0,sizeof(x))
 3 #define clr_1(x) memset(x,-1,sizeof(x))
 4 #define INF 0x3f3f3f3f
 5 #define LL long long
 6 #define pb push_back
 7 #define ls(i) (i<<1)
 8 #define rs(i) (i<<1|1)
 9 #define mp make_pair
10 #define fi first
11 #define se second
12 using namespace std;
13 const int N=(1<<18)+10;
14 const int maxn=1<<18;
15 int n,q;
16 LL bits[N];
17 LL a[N];
18 void add(int i,LL x)
19 {
20     while(i<=maxn)
21     {
22         bits[i]+=x;
23         i += i & -i;
24     }
25     return ;
26 }
27 LL sum(int i)
28 {
29     LL ans=0;
30     while(i)
31     {
32         ans+=bits[i];
33         i -= i & -i;
34     }
35     return ans;
36 }
37 int upt(LL x)
38 {
39     if(bits[maxn]<=x) return INF;
40     int p=0;
41     LL res=0;
42     for(int i=17;i>=0;i--)
43         if(res+bits[p|(1<<i)]<=x)
44         {
45             res+=bits[p|(1<<i)];
46             p|=(1<<i);
47         }
48     return p+1;
49 }
50 int main()
51 {
52     scanf("%d%d",&n,&q);
53     for(int i=1;i<=n;i++)
54     {
55         scanf("%I64d",a+i);
56         add(i,a[i]);
57     }
58     while(q--)
59     {
60         int pos;
61         LL val;
62         scanf("%d%I64d",&pos,&val);
63         add(pos,val-a[pos]);
64         a[pos]=val;
65         LL tmp=a[1];
66         LL ans=-1;
67         if(tmp==0)
68         {
69             printf("1\n");
70             continue;
71         }
72         while(tmp<INF)
73         {
74             int k=upt(2*tmp-1);
75             if(k==INF)
76                 break;
77             if(sum(k-1)==a[k])
78             {
79                 ans=k;
80                 break;
81             }
82             tmp=sum(k-1)+a[k];
83         }
84         printf("%I64d\n",ans);
85     }
86     return 0;
87 }
View Code

 

相关文章:

  • 2021-04-24
  • 2021-12-04
  • 2021-11-19
  • 2021-04-27
  • 2021-08-14
  • 2021-08-20
  • 2021-06-21
猜你喜欢
  • 2022-12-23
  • 2021-09-09
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-08-12
相关资源
相似解决方案