codeforce Gym - 101597C. Candy division
Your older sister has three kids. Whenever you go to visit her, you bring along a bag of candies for the kids. There are n candies in the bag. You want to give all the candies to the kids, but you also want to teach them a little math along the way. Therefore, you gave them not just the bag of candies but also one simple rule: each of the kids must take an integer fraction of candies in the bag. In other words, the amount of candies each kid takes must be a divisor of n.
Formally, in order to divide all the candies the kids have to find three positive integers a1, a2, a3 such that n = a1 + a2 + a3 and each aidivides n.
The first line of the input contains a single integer t – the number of test cases to follow. Each of the following t lines of the input contains the integer n. You may assume that 1 ≤ t ≤ 100 and 1 ≤ n ≤ 1018.
Output t lines. The k-th line will solve the k-th test case and will contain three integers ai as specified above. If there are multiple solutions you may select an arbitrary one. If there are no solutions, output the word 'IMPOSSIBLE' instead (quotes only for clarity).
3 1 3 12
IMPOSSIBLE 1 1 1 4 6 2
已知一个数n,求是否存在abc,使a+b+c=n(1)的同时~a,b,c都能被n整除;
假设a*d=b*e=c*f=n; 把这个式子分别代入(1)式~~这样的话就是n/d+n/e+n/f=n~~简化得:1/d+1/e+1/f=1;
(其中d,e,f都是整数)~~这样的话~~把式子化为1/d+1/e=1-(1/f); 这样当f>=1的时候~~右边值就是0,1/2,2/3,3/4,4/5.......等等~~ 也就是1/d+1/e=(0, 1/2 , 2/3 , 3/4 , 4/5 ......)我们可以找出~~在d,e都为整数的时候~~又能够凑出1/2和2/3这两个值~~此时d,e,f的值分别为:3 3 3 (1/3, 1/3 ,1/3),4 4 2(1/4,1/4,1/2);
也就是::当这个数n能被3或4整除的时候~~才能化成如题目的形态~~;
最后别忘了用long long~~
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
while (n--)
{
long long int m;
scanf("%lld", &m);
if (m % 3 == 0)
{
cout << m / 3 << " " << m / 3 << " " << m / 3 << endl;
}
else if (m % 4 == 0)
{
cout << m / 2 << " " << m / 4 << " " << m / 4 << endl;
}
else
{
cout << "IMPOSSIBLE" << endl;
}
}
}