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.
The first line contains a single integer n (1 ≤ n ≤ 105) — the size of the array.
The second line contains n integers a1, a2, ..., an ( - 105 ≤ ai ≤ 105) — the elements of the array.
Print a single integer — the minimum number of seconds needed to make all elements of the array equal to zero.
5
1 1 1 1 1
1
3
2 0 -1
2
4
5 -6 -5 1
4
In the first example you can add - 1 to all non-zero elements in one second and make them equal to zero.
In the second example you can add - 2 on the first second, then the array becomes equal to [0, 0, - 3]. On the second second you can add 3 to the third (the only non-zero) element.
判断除0外不同数字的个数set即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include<bits/stdc++.h> #include<set> #include<map> #include<list> #include<queue> #include<stack> #include<vector> #include<bitset> using namespace std; //const int N=; typedef long long ll; int a[1100]; set<int>aa; int main(){ std::ios::sync_with_stdio(false); int n,m,tmp;cin>>n;int f=0; for(int i=1;i<=n;i++){ cin>>tmp;aa.insert(tmp); if(tmp==0) f=1; } cout<<aa.size()-f; return 0; }
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 (a, b) good, if GCD(a, b) = x and LCM(a, b) = y, where GCD(a, b) denotes the greatest common divisorof a and b, and LCM(a, b) denotes the least common multiple of a and b.
You are given two integers x and y. You are to find the number of good pairs of integers (a, b) such that l ≤ a, b ≤ r. Note that pairs (a, b)and (b, a) are considered different if a ≠ b.
The only line contains four integers l, r, x, y (1 ≤ l ≤ r ≤ 109, 1 ≤ x ≤ y ≤ 109).
In the only line print the only integer — the answer for the problem.
1 2 1 2
2
1 12 1 12
4
50 100 3 30
0
In the first example there are two suitable good pairs of integers (a, b): (1, 2) and (2, 1).
In the second example there are four suitable good pairs of integers (a, b): (1, 12), (12, 1), (3, 4) and (4, 3).
In the third example there are good pairs of integers, for example, (3, 30), but none of them fits the condition l ≤ a, b ≤ r.
因为乘积等于某个值的时候可以用sqrt优化,两个数一定是一个大于sqrt,一个小于sqrt,或者两者均等于sqrt
令 ax,bx 代表 原题的a,b>>>同时需要保证gcd(a,b)==1
则a*x*b*x=x*y>>>>>>a*b=y/x; 根号(y/x)暴力即可,注意>>没搞明白>>为甚麽会有y%x!=0的情况
代码中的一些小地方,比如 for(int a=1;a*a<tmp;a++) 多用乘法转换
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include<bits/stdc++.h> #include<set> #include<map> #include<list> #include<queue> #include<stack> #include<vector> #include<bitset> using namespace std; typedef long long ll; ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); } int main(){ ll l,r,x,y; std::ios::sync_with_stdio(false);cin.tie(0); cin>>l>>r>>x>>y;ll tmp=y/x;ll ans=0; if(y%x){ cout<<"0\n";return 0; } for(ll a=1;a*a<=tmp;a++){ ll b=tmp/a; if(tmp%a!=0) continue; if(a*x<l||a*x>r) continue; if(b*x<l||b*x>r) continue; if(gcd(a,b)==1) {ans+=2;if(a==b) ans--;} } cout<<ans<<endl; return 0; }
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 x dresses now, so she became interested in the expected number of dresses she will have in one year. Nastya lives in Byteland, so the year lasts for 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.
The only line contains two integers x and k (0 ≤ x, k ≤ 1018), where x is the initial number of dresses and k + 1 is the number of months in a year in Byteland.
In the only line print a single integer — the expected number of dresses Nastya will own one year later modulo 109 + 7.
2 0
4
2 1
7
3 2
21
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 3 dresses with 50% probability and 4 dresses with 50% probability. Thus, in the end of the year there are 6 dresses with 50% probability and 8 dresses with 50% probability. This way the answer for this test is (6 + 8) / 2 = 7.
用dp推出等比数列通项公式即可>>>Ak=A1*q^(n-1)>>头脑清晰即可>>注意数据防爆
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mod=1e9+7; ll pow_(ll b){ ll res=1,tmp=2; while(b){ if(b&1) (res*=tmp)%=mod; b/=2; (tmp*=tmp)%=mod; } return res; } int main(){ std::ios::sync_with_stdio(false); ll x,k;cin>>x>>k; if(k==0){ ll ans=2*x;ans%=mod; cout<<ans<<endl; return 0; } if(x==0){ cout<<"0"<<endl; return 0; } ll ans=pow_(k); ll tmp=(2*x-1);tmp%=mod; ans*=tmp;ans%=mod; ans+=1;ans%=mod; cout<<ans<<endl; return 0; }
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 , where p is the product of all integers on the given array, s is their sum, and 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.
The first line contains two integers n and k (1 ≤ n ≤ 2·105, 1 ≤ k ≤ 105), where n is the length of the array and k is the constant described above.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 108) — the elements of the array.
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.
1 1
1
1
4 2
6 3 8 1
2
In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because .
There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because . Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because
.
思维好题>>>怎末说呢>>>这体现了一种做题的方法吧>>>暴力之后再去想优化>>>
注意到正数的乘法最多跳除1外60次>>>暴力即可
>>同时注意乘法是否会溢出>>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+10; ll sum[N],a[N];int nxt[N]; const ll INF=0x3f3f3f3f3f3f3f3f; int main(){ std::ios::sync_with_stdio(false);cin.tie(0); int n,k;cin>>n>>k; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; nxt[n+1]=n+1; for(int i=n;i>=1;i--){ nxt[i]=nxt[i+1]; if(a[i]>1) nxt[i]=i; } ll ans=0; for(int i=1;i<=n;i++){ ll s=a[i],p=a[i]; if(s*k==p) ans++; for(int j=i+1;j<=n;j++){ if(a[j]!=1){ if(INF/a[j]<=p) break; p*=a[j];s+=a[j]; if(s*k==p) ans++; } else{ int num=nxt[j]-j; if(s*k<p&&p%k==0&&(s+num)*k>=p) ans++; s+=num;j+=num-1; } } } cout<<ans<<endl; return 0; }