On the most perfect of all planets i1c5l various numeral systems are being used during programming contests. In the second division they use a superfactorial numeral system. In this system any positive integer is presented as a linear combination of numbers converse to factorials:
Here a1 is non-negative integer, and integers ak for k?≥?2 satisfy 0?≤?ak?<?k. The nonsignificant zeros in the tail of the superfactorial number designation are rejected. The task is to find out how the rational number is presented in the superfactorial numeral system.
Input
Single line contains two space-separated integers p and q (1?≤?p?≤?106, 1?≤?q?≤?106).
Output
Single line should contain a sequence of space-separated integers a1,?a2,?...,?an, forming a number designation in the superfactorial numeral system. If several solution exist, output any of them.
题意:给你p和q,叫你找出所有ai使得等式成立
思路:因为有阶乘所以不能暴力求解,可以先把q乘过去,就变成了p=a1*q+a2*q/2!+...,很明显此时常数项a1=p/q,然后减去该项,将等式两边同时*2,此时a2就变成了常数项,求出a2再减掉,两边同时*3,a3也变成了常数项,以此类推。所以只要枚举n就行了。具体看代码。
1 #include<iostream> 2 #include<algorithm> 3 #include<cmath> 4 using namespace std; 5 int a[1000000]={0}; 6 int main() 7 { 8 long long int p,q; 9 cin>>p>>q; 10 int t; 11 for(int i=1;i<1000000;i++) 12 { 13 p*=i; 14 a[i]=p/q; 15 p=p%q; 16 if(p==0) 17 { 18 t=i; 19 break; 20 } 21 } 22 cout<<a[1]; 23 for(int i=2;i<=t;i++) 24 cout<<‘ ‘<<a[i]; 25 cout<<endl; 26 }
原文地址:https://www.cnblogs.com/spongeb0b/p/9350163.html