Tutorials, Free Online Tutorials, publishbookmarks provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.
lyk likes to study some difficult problems, such as NP issues.
This time it encountered another tricky NP problem. The problem is like this: there are two numbers n and p, find the step of n
The result of picker p.
LYK thinks all NP problems are algorithm without polynomial complexity, so it intends to ask for help to participate in NOIP
You, help LYK!
Input format (np.in)
Input a line of two integer n, p.
output format (np.out)
Output a line of an integer to represent the answer.
Input sample
3 4
output sample
2
data range
For 20%data: n, p <= 5.
For 40%data: n, p <= 1000.
For 60%data: n, p <= 10000000.
For 80%data: n <= 10^18, P <= 10000000.
For another 20%data: n <= 10^18, P = 10000007.
Of these, 50%of the data meets n> = p.
Thinking:
Blocking Table
All K!, Where K Mod 10000000 = 0. Find the known answer directly when you ask, and you can find the answer without more than 100,000.
lyk is currently preparing NOIP2017's preliminary round. What is the least good at is to see the program and write the results, so it desperately
Practicing.
This time it gets such a program:
Pascal:
Readln (n);
for i: = 1 to n do read (a [i]);
For I: = 1 to n do for j: = 1 to n do for k: = 1 to n do for l: = 1 to n down
if (a [i] = a [j]) and (a [i] <a [k]) and (a [k] = a [l]) then ans: = (ang 1) MOD 1000000007;
writeln (ANS);
C ++:
scanf ("%d", & n);
for (i = 1; i <= n; i ++) scanf ("%d", & a [i]);
For (i = 1; i <= n; i ++) for (j = 1; j <= n; j ++) for (k = 1; k <= n; k ++) for (l = 1; l <= n; L ++)
if (a [i] == a [j] && a [i] <a [k] && a [k] == a [l]) ans = (ang 1)%1000000007;
Printf ("%d \ n", ANS);
LYK knows all the input data, and it wants to know how much this program runs.
Input format (Program.in)
The first line of the number n, the second line n, represent AI.
output format (program.out)
A number indicates the answer.
Input sample
4
1 1 3 3
output sample
4
Data range
For 20%data n <= 50.
For 40%data n <= 200.
For 60%data n <= 2000.
For 100%data n <= 100000, 1 <= AI <= 1000000000.
Among them, 50%of the number of AIs with different data <= 10 is evenly distributed, and the number of AIs with different data of 50%of the data> = n/10.
–
Thinking:
The principle of multiplication is used
Sort the AI first.
Let f [i] represent the same AI pair in 1 ~ i.
For the position of all AI! = A {i+1}, the f [i]*g*(g-1)/2 is added into the answer. G represents how much number exists = a {i+1}.
Time complexity NLGN.
code:
#include <iostream>#include <cstdio>#include <algorithm>
using namespace std;
longlong ans,f[100005];
int i,n,a[100005],sum;
const int MOD=1000000007;
int cmp(int i,int j) {
return i<j;}
int main()
{
freopen("program.in","r",stdin);
freopen("program.out","w",stdout);
scanf("%d",&n);
for (i=1; i<=n; i++) scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp); sum=1; f[1]=1;
for (i=2; i<=n; i++)
{
f[i]=f[i-1];
if (a[i]==a[i-1])
{
f[i]=f[i]-1ll*sum*sum; sum++;
f[i]=f[i]+1ll*sum*sum;
f[i]%=MOD;
} else {
sum=1; f[i]++;}
}
sum=0;
for (i=n; i>=2; i--)
{
if (a[i]==a[i+1]) sum++; elsesum=1;
if (a[i]!=a[i-1]) ans=(ans+f[i-1]*sum%MOD*sum)%MOD;
}
cout<<ans<<endl;
return0;
}
Select numbers (Select)
Time Limit:3000ms Memory Limit:64MB
Title description
lyk found a matrix of N*M. There are some numbers on this matrix.
The number is AI, J.
Because it was AK's preliminary round of NOIP2016, it seemed very boring recently, and I thought of a way to entertain and entertain it.
The game it thinks is like this: every time you choose one or a column, the happiness value it gets will be this line or a column
The sum of numbers. After that, it subtracted the number of the line or on the column (may be turned into negative numbers later). So, repeat K K
Second, the sum of the happiness it gets will be its NOIP2016 rematch.
Lyk certainly wants its RP value to be as high as possible, so it will help you.
Input format (Select.in)
The first line of 4 numbers n, m, k, p.
Next N line M column, representing AI, J.
output format (Select.out)
Output a line represents the maximum RP value.
Input sample
2 2 5 2
1 3
2 4
output sample
11
Data range
A total of 10 sets of data.
For groups 1, 2 data n, m, k <= 5.
For the third group of data K = 1.
For the 4th group of data p = 0.
For groups 5,6 data n = 1, m, k <= 1000.
For group 7,8 data n = 1, m <= 1000, k <= 1000000.
For all data 1 <= n, m <= 1000, K <= 1000000, 1 <= AI, J <= 1000, 0 <= P <= 100.
Sample Explanation
Choose the second column for the first time, the second line of the second line, the third time the first line, the second line of the second line, the fifth
Select the first line, the happiness value is 7+4+2+0+-2 = 11.
Thinking:
The order of choice is irrelevant. Assuming that we chose line X, then the K-X column must be selected.
Let f [i] indicate the maximum sum to choose I. Consider how to find all f [i].
This is obviously greedy. Record the sum of all lines. Just use the big roots to maintain.
Let G [i] indicate the maximum selection of I column. Known F and G.
The answer is Max {f [i]+g [k-i] -I*(k-I)*p).
code:
#include <cmath>#include <cstdio>#include <cstdlib>#include <iostream>#include <algorithm>usingnamespacestd;
constint N=1000005;
longlong ans,s1[N],s2[N],p1[N],p2[N];
int k,n,m,i,j,p,A;
int cmp(int i,int j) {
return i<j;}
int main()
{
freopen("select.in","r",stdin);
freopen("select.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&k,&p);
for (i=1; i<=n; i++)
for (j=1; j<=m; j++)
{
scanf("%d",&A);
s1[i]+=A; s2[j]+=A;
}
make_heap(s1+1,s1+n+1,cmp);
make_heap(s2+1,s2+m+1,cmp);
for (i=1; i<=k; i++)
{
p1[i]=p1[i-1]+s1[1];
pop_heap(s1+1,s1+n+1,cmp);
s1[n]-=p*m;
push_heap(s1+1,s1+n+1,cmp);
p2[i]=p2[i-1]+s2[1];
pop_heap(s2+1,s2+m+1,cmp);
s2[m]-=p*n;
push_heap(s2+1,s2+m+1,cmp);
}
ans=1ll*-1000000000*1000000000;
for (i=0; i<=k; i++) {ans=max(ans,p1[i]+p2[k-i]-1ll*i*(k-i)*p);}
cout<<ans;
return0;
}