Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Print the minimum money needed to keep Duff happy for n days, in one line.
3 1 3 2 2 3 1
10
3 1 3 2 1 3 2
8
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
题意:给定一个天数n,然后给出每天需要的肉量和当前肉的价格,(肉可以多买,多余的留到以后吃)问这n天买肉的价钱的最小值
思路:只要判断如果今天没肉了,那么今天肯定要买今天所需的肉,然后如果今天的肉价比明天低,那么在今天把明天的分量也
买了,如果今天的肉价比后天也便宜,继续买后天所需的肉,以此类推即可。
#include<iostream> #include<cstring> #include<string> #include<stdio.h> using namespace std; const int MAXN=100005; int a[MAXN],p[MAXN],vis[MAXN]; int main() { int n; long long int mincost=0; memset(vis,0,sizeof(vis)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%lld %lld",&a[i],&p[i]); } for(int i=1;i<=n;i++) { if(!vis[i]) { mincost+=(a[i]*p[i]); vis[i]=1; for(int j=i+1;j<=n;j++) { if(p[i]<p[j]) { mincost+=(a[j]*p[i]); vis[j]=1; } else { break; } } } } printf("%lld\n",mincost); return 0; }
Duff is in love with lovely numbers! A positive integer x is called lovely if and only if there is no such positive integera > 1 such that a2 is a divisor of x.
Malek has a number store! In his store, he has only divisors of positive integer n (and he has all of them). As a birthday present, Malek wants to give her a lovely number from his store. He wants this number to be as big as possible.
Malek always had issues in math, so he asked for your help. Please tell him what is the biggest lovely number in his store.
The first and only line of input contains one integer, n (1 ≤ n ≤ 1012).
Print the answer in one line.
10
10
12
6
In first sample case, there are numbers 1, 2, 5 and 10 in the shop. 10 isn't divisible by any perfect square, so 10 islovely.
In second sample case, there are numbers 1, 2, 3, 4, 6 and 12 in the shop. 12 is divisible by 4 = 22, so 12 is notlovely, while 6 is indeed lovely.
思路:将n的素因子想乘即可
#include<iostream> #include<cmath> using namespace std; int main() { long long int n,temp=1; cin >> n; for (int i = 2; i <= sqrt(double(n)); i++) { if (n%i == 0) { temp *= i; while (n%i == 0) { n /= i; } } } cout << temp*n << endl; //system("pause"); return 0; }
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
The first line of input contains integer n (1 ≤ n ≤ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 ≤ wi ≤ 106 for each 1 ≤ i ≤ n), the powers of two forming the weights values.
Print the minimum number of steps in a single line.
5 1 1 2 3 3
2
4 0 1 2 3
4
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
题意:给定n个哑铃,每个重量为2^n[i] 每次可以拿起若干个,但是这若干个要满足加起来的和是2的次方,问最少需要几次能举完这些哑铃
思路:题目的意思相当于两个重量为2^i的哑铃组合在一起变成2^i+1。只要一直组合,直到不能组成那么算一步,然后剩下的数继续次操作
用优先队列很好解决。
#include<iostream> #include<vector> #include<queue> #include<stdio.h> using namespace std; const int MAXN=1000005; struct cmp { bool operator ()(int &a,int &b) { return a>b; } }; int main() { int n,val; scanf("%d",&n); priority_queue<int,vector<int>,cmp> Q; for(int i=0;i<n;i++) { scanf("%d",&val); Q.push(val); } int step=0; while(!Q.empty()) { int head,next; head=Q.top(); Q.pop(); if(Q.empty()) { step++; break; } next=Q.top(); Q.pop(); if(head==next) { Q.push(head+1); } else { step++; Q.push(next); } } printf("%d\n",step); return 0; }
While Duff was resting in the beach, she accidentally found a strange array b0, b1, ..., bl - 1 consisting of l positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array,a0, ..., an - 1 that b can be build from a with formula: bi = ai mod n where a mod b denoted the remainder of dividing aby b.
Duff is so curious, she wants to know the number of subsequences of b like bi1, bi2, ..., bix (0 ≤ i1 < i2 < ... < ix < l), such that:
Since this number can be very large, she want to know it modulo 109 + 7.
Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number.
The first line of input contains three integers, n, l and k (1 ≤ n, k, n × k ≤ 106 and 1 ≤ l ≤ 1018).
The second line contains n space separated integers, a0, a1, ..., an - 1 (1 ≤ ai ≤ 109 for each 0 ≤ i ≤ n - 1).
Print the answer modulo 1 000 000 007 in one line.
3 5 3 5 9 1
10
5 10 3 1 2 3 4 5
25
In the first sample case, . So all such sequences are: , , , , , , , , and .
(没写orz,解题思路来自acm吧的讨论贴)
Recently Duff has been a soldier in the army. Malek is her commander.
Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.
There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number ofi - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city.
Malek loves to order. That's why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.
To answer a query:
Assume there are x people living in the cities lying on the path from city v to city u. Assume these people's IDs arep1, p2, ..., px in increasing order.
If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, ..., pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).
Duff is very busy at the moment, so she asked you to help her and answer the queries.
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105).
The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n,v ≠ u).
Next line contains m integers c1, c2, ..., cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m).
Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).
For each query, print numbers k, p1, p2, ..., pk separated by spaces in one line.
5 4 5 1 3 1 2 1 4 4 5 2 1 4 3 4 5 6 1 5 2 5 5 10 2 3 3 5 3 1
1 3 2 2 3 0 3 1 2 4 1 2
Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):
(没写orz,解题思路来自acm吧的讨论贴)