A. Nastya and an Array
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的个数
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 set<int> st; 5 int main() { 6 int n, x; 7 cin >> n; 8 for(int i = 1; i <= n; i ++) { 9 cin >> x; 10 if(x != 0) st.insert(x); 11 } 12 cout << st.size() << endl; 13 return 0; 14 }
B. Nastya Studies Informatics
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.
sqtr(y*x),循环一下
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 ll gcd(ll x, ll y) { 5 return y?gcd(y,x%y):x; 6 } 7 int main() { 8 ll x, y, l ,r, ans = 0; 9 cin >> l >> r >> x >> y; 10 for(ll i = x; i*i <= x*y; i += x) { 11 if(x*y%i == 0) { 12 ll j = x*y/i; 13 if(l <= i && i <= r && l <= j && j <= r) 14 if(gcd(i, j) == x) 15 ans += 1 + (i!=j); 16 } 17 } 18 cout << ans << endl; 19 return 0; 20 }
C. Nastya and a Wardrobe
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.
可以推算出来是2*(x^k-2^k)+1,等于0时特判下。
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 const ll mod = 1e9+7; 5 6 ll pow_mod(ll x, ll n) { 7 ll ans = 1; 8 while(n) { 9 if(n&1) ans = ans*x%mod; 10 x = x*x%mod; 11 n /= 2; 12 } 13 return ans; 14 } 15 16 int main() { 17 ll x, k; 18 cin >> x >> k; 19 if(k == 0) return 0*printf("%lld ",x*2%mod); 20 if(x == 0) return 0*printf("0 "); 21 cout << ((2*x-1)%mod*pow_mod(2,k)+1)%mod << endl; 22 return 0; 23 } 24 // 2*(x^k-2^k)+1