Idea: This problem seems to be a greedy, but this greed is obviously wrong. In fact, the problem is very simple, first determine whether 1 can, and then determine whether 2 can. Then find the smallest< Span class= "Katex-mathml" style= "Position:absolute; padding:0px; border:0px; height:1px; width:1px; Overflow:hidden ">k (k > 2) k ( k > 2 Makes(m-k) mod 6 = 0 (m? k )mod6=0 Can.
The proof is as follows:3n (n-1) +1 = 6 (n (n-1)/2) +13N(N?1)+1=6(N?(N?1)/2)+1, noting thatN (n-1)/2 n ? (n? 1 )/2 is the number of triangles, any natural number can only be represented by a maximum of 3 triangles. Enumeration requires kk One, then obviously m=6 (k m = 6 (k The number of triangles and ) +k ) + k Because k \ge 3 k ≥ 3 Just m-k m? k is a multiple of 6 must be a solution.
In fact, a table should also be able to find patterns.
The two sides to the middle of a scan when the wrong number of times, forget the equal time can also, finally find the smallest k, directly take the rest on the line, at least starting from 3.
#include <bits/stdc++.h> #pragma comment (linker, "/stack:1024000000,1024000000") #define INF 9999999999#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef unsigned long long llu;typedef long long ll;const int maxd=18258 +5;///=========================int s[maxd];int m;void table () {s[0]=0; for (int i=1;i<maxd;i++) s[i]=3*i* (i-1) +1;} BOOL Ok1 (int x) {int pos=lower_bound (s,s+maxd,x)-S; if (s[pos]==x) return true; return false;} BOOL Ok2 (int x) {if ((x-2)%6) return false; int l=1,r=maxd-1; while (l<=r) {if (s[l]+s[r]<x) l++; else if (s[l]+s[r]>x) r--; else return true; } return false;} int main () {int kase; Freopen ("1.txt", "R", stdin); Table (); scanf ("%d", &kase); while (kase--) {scanf ("%d", &m); if (Ok1 (m)) printf ("1\n"); else if (Ok2 (m)) printf ("2\n"); else {//for (int k=3;k<=m;k++)// if ((m-k)%6==0)//{//printf ("%d\n", k);//break;//} printf ("%d\n", (m-3)%6+3); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
hdu--5312 (Law + mathematics)