Sequenceaccepts:25submissions:1442Time limit:2000/2000 MS (java/others)Memory limit:262144/262144 K (java/others)Problem description
Soda learned a series, the first of the seriesNN(n \ge 1)(N≥1) Item is3n (n-1) +13N(N−1)+1. Now he wants to know for a given integerMm, can be expressed as a number of the above-mentioned series and . If so, what is the minimum number of items you need? For example, 22 can be represented as 7+7+7+1< span class= "Strut" >7+7+7+ 1, can also be expressed as 19+1+1+1< span class= "Strut" >19+1+1 +1.
Enter a description
Enter more than one set of data. The first line has an integerTT(1 \le T \le 10^4)(1≤T≤10?4??) that represents the number of test data groups. Then for each set of data: one row contains 1 integersmm (1 \le m \le 10^9)(1≤m≤10? ) 9?? ).
Output description
Minimum cost for each set of data output.
Input sample
10123456782210
Output sample
1234561244
The game didn't work out. An important property to be used in this problem is that any natural number can be represented as many as three triangles (1,3,6,10,15 ...) and (Orz Gauss) Then there is also the conclusion that the generalization to any natural number can be expressed as a K-shaped number of K-Angles (Fermat presents the conjecture, Cauchy gave proof) then the official puzzle is better:
The problem seems to be a greedy one, but the greedy is obviously wrong. In fact, the problem is very simple, first determine whether 1 can, and then determine whether 2 can. Then find the smallestK ( k > 2)k(k>2), making (m-k) mod 6 = 0(m−k)mo< C25>d6=0.
The proof is as follows:3n (n-1) +1 = 6 (n (n-1)/2) +13N(N−1)+1=6(N∗(N−1)/2)+1, noticeN (n-1)/2N∗(N−1)/2 is the number of triangles, any natural number can only be represented by a maximum of 3 triangles. Enumeration requiresKK, then obviouslyM=6 (kM=6 (k a number of triangles and ) +k+k due to k \ge 3k≥3, as long as m-k m −k is a multiple of 6 must be a solution.
In fact, a table should also be able to find patterns.
There is also a point, a special sentence for a and two cases, a good judgment, sweep it all over.
Two, because this sequence is incremented, we can go from both sides to the middle, is a good optimization, see the code.
/************************************************************************* > File name:code/nv/#ann/1003.cpp > Author:111qqz > Email: [email protected] > Created time:2015 July 28 Tuesday 23:03 09 seconds ********************* ***************************************************/#include<iostream>#include<iomanip>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<string>#include<map>#include<Set>#include<queue>#include<vector>#include<stack>#defineY0 ABC111QQZ#defineY1 HUST111QQZ#defineYn hez111qqz#defineJ1 CUTE111QQZ#defineTM CRAZY111QQZ#defineLR DYING111QQZusing namespacestd;#defineREP (i, n) for (int i=0;i<int (n); ++i)typedefLong Longll;typedef unsignedLong LongULL;Const intINF =0x7fffffff;Const intn=1e5+7;intK,m,f[n];voidinit () { for(inti =1; I <N; i++) {F[i]=3*i* (I-1)+1; if(f[i]>1000000000) {k= I1; Break; } }}intSolve (intx) { for(inti =1; F[i]<=x; i++ ) { if(x==F[i])return 1; } intj =K; for(inti =1; I <= K1&&f[i]<x; i++)//Because the sequence is incremented, so it can be written. { while(f[i]+f[j]>x) j--; if(f[i]+f[j]==x)return 2; } for(inti =3; I <= m; i++ ) { if((m-i)%6==0) returni; }}intMain () {intT; Init (); CIN>>T; intans; while(t--) {scanf ("%d",&m); cout<<solve (m) <<Endl; } return 0;}
(BC first anniversary) HDU 5312 Sequence