Just the Facts

本文介绍了一个算法,用于计算从1阶乘到指定数字N的末尾非零数字,通过逐次更新并取模操作来避免溢出问题。

Just the Facts

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 9
Problem Description
The expression N!, read as "N factorial," denotes the product of the first N positive integers, where N is nonnegative. So, for example, 
N N! 
0 1 
1 1 
2 2 
3 6 
4 24 
5 120 
10 3628800 
For this problem, you are to write a program that can compute the last non-zero digit of any factorial for (0 <= N <= 10000). For example, if your program is asked to compute the last nonzero digit of 5!, your program should produce "2" because 5! = 120, and 2 is the last nonzero digit of 120.
 

 

Input
Input to the program is a series of nonnegative integers not exceeding 10000, each on its own line with no other letters, digits or spaces. For each integer N, you should read the value and compute the last nonzero digit of N!.
 

 

Output
For each integer input, the program should print exactly one line of output. Each line of output should contain the value N, right-justified in columns 1 through 5 with leading blanks, not leading zeroes. Columns 6 - 9 must contain " -> " (space hyphen greater space). Column 10 must contain the single last non-zero digit of N!.
 

 

Sample Input
1
2
26
125
3125
9999
 

 

Sample Output
    1 -> 1
    2 -> 2
   26 -> 4
  125 -> 8 
 3125 -> 2 
 9999 -> 8
 

 

Source
PKU
输入一个数字N,计算1阶乘到N,求出最末位非0数字输出。
该题的难点在意,需要在每一次的阶乘的时候(ID[i]+=(ID[i-1]*i);),把末尾非零的数组都去掉,保留非零数值,并且最后还得取模10000,防止下一次数据相乘过大溢出,依次更新到题目所要求的最大范围10000。即可。在每次输出的时候,就可以直接输出该末尾非零数值了的。
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define CLR(a) memset(a,0,sizeof(a))/*初始化*/
 5 main()
 6 {
 7    int Num,ID[11000],i;
 8    CLR(ID);
 9    ID[0]=0;ID[1]=1;
10    for(i=1;i<=10000;i++)
11     {
12         ID[i]+=(ID[i-1]*i);     /*保存每一次阶乘的数值*/
13         while(ID[i]%10==0&&ID[i]>=10)/*记录每一个末尾非零数值*/
14             ID[i]/=10;
15         ID[i]=ID[i]%100000;     /*防止下次数据相乘过大,溢出*/
16     }
17     while(scanf("%d",&Num)!=EOF)
18     {
19         printf("%5d -> %d\n",Num,ID[Num]%10);/*直接输出该阶乘结果的个位数*/
20     }
21     return 0;
22 }
View Code

 

转载于:https://www.cnblogs.com/Wurq/articles/3929183.html

Compared with homogeneous network-based methods, het- erogeneous network-based treatment is closer to reality, due to the different kinds of entities with various kinds of relations [22– 24]. In recent years, knowledge graph (KG) has been utilized for data integration and federation [11, 17]. It allows the knowledge graph embedding (KGE) model to excel in the link prediction tasks [18, 19]. For example, Dai et al. provided a method using Wasser- stein adversarial autoencoder-based KGE, which can solve the problem of vanishing gradient on the discrete representation and exploit autoencoder to generate high-quality negative samples [20]. The SumGNN model proposed by Yu et al. succeeds in inte- grating external information of KG by combining high-quality fea- tures and multi-channel knowledge of the sub-graph [21]. Lin et al. proposed KGNN to predict DDI only based on triple facts of KG [66]. Although these methods have used KG information, only focusing on the triple facts or simple data fusion can limit performance and inductive capability [69]. Su et al. successively proposed two DDIs prediction methods [55, 56]. The first one is an end-to-end model called KG2ECapsule based on the biomedical knowledge graph (BKG), which can generate high-quality negative samples and make predictions through feature recursively propagating. Another one learns both drug attributes and triple facts based on attention to extract global representation and obtains good performance. However, these methods also have limited ability or ignore the merging of information from multiple perspectives. Apart from the above, the single perspective has many limitations, such as the need to ensure the integrity of related descriptions, just as network-based methods cannot process new nodes [65]. So, the methods only based on network are not inductive, causing limited generalization [69]. However, it can be alleviated by fully using the intrinsic property of the drug seen as local information, such as chemical structure (CS) [40]. And a handful of existing frameworks can effectively integrate multi-information without losing induction [69]. Thus, there is a necessity for us to propose an effective model to fully learn and fuse the local and global infor- mation for improving performance of DDI identification through multiple information complementing.是什么意思
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值