码迷,mamicode.com
首页 > 其他好文 > 详细

Gym - 100637J

时间:2018-07-22 17:03:45      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:ace   ring   eve   cte   perfect   contain   ==   nta   osi   

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?≤?1061?≤?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 }

 

Gym - 100637J

标签:ace   ring   eve   cte   perfect   contain   ==   nta   osi   

原文地址:https://www.cnblogs.com/spongeb0b/p/9350163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!