1. 程式人生 > >A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【數學】

A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【數學】

pos == force line for 最大 header 應該 XML

題目:

Little C loves number ?3? very much. He loves all things about it.

Now he has a positive integer nn. He wants to split nn into 3 positive integers a,b,ca,b,c, such that a+b+c=na+b+c=n and none of the 3 integers is a multiple of 3. Help him to find a solution.

Input

A single line containing one integer

nn (3n10^9) — the integer Little C has.

Output

Print 3 positive integers a,b,c in a single line, such that a+b+c=n and none of them is a multiple of 3

It can be proved that there is at least one solution. If there are multiple solutions, print any of them.

Examples input
3
output
1 1 1
input
233
output
77 77 79


題意分析:
這題是一個比較單純的數學題目,給你一個數n,你需要把他分解成3個數,,並且這3個數都不是3的倍數。
這題我想的是根據數的素數分解原理,因為每個數都可以表示成素數相乘。所以對於N,
如果N的素因子中沒有3,那麽我們另外兩個數只要相加等於3的倍數,那麽就一定是滿足的。
如果N的素因子中有3,那麽此時,3應該還有個最大冪指數t,並且3的t次冪是N的一個因子。現在就是對3的t次冪的分解。假設 b = N/(3^t)
對3的t次冪分解成3個不被3整除的數還是比較簡單的,因為是3的次冪,所以肯定可以分解成3個3的(t-1)次冪,那麽 其中任意兩個數減去1,另外一個數加上1就滿足了,再把這3個數都乘以b即滿足。


代碼:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int N;
    while(~scanf("%d", &N))
    {
        int cnt = 1;
        if(N%3 != 0)  //N不是3的倍數
        {
            printf("%d %d %d\n", 1, 2, N-3);
            continue;
        }
        while(N%3 == 0) //提取N中3的t次冪因子。
        {
            cnt*=3;
            N/=3;
        }
        int a, b, c, d;
        d = cnt/3;
        if(d%3==0)
        {
            a = (d-1)*N;
            b = (d+2)*N;
            c = (d-1)*N;
        }
        else  //d==1
        {
            a = b = c = d*N;
        }
        printf("%d %d %d\n", a, b, c);

    }
    return 0;
}

  

A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【數學】